001    package railo.commons.io.res.type.ram;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.List;
006    
007    /**
008     * Core of a Ram Resource, holds the concrete data for a existing resource
009     */
010    public final class RamResourceCore implements Serializable {
011    
012            /**
013             * Directory Resource
014             */
015            public static final int TYPE_DIRECTORY=1;
016    
017            /**
018             * Directory Resource
019             */
020            public static final int TYPE_FILE=2;
021    
022            private static final String[] EMPTY_NAMES = new String[0];
023            
024            private int type;
025            private String name;
026            private byte[] data;
027            private List children;
028            private long lastModified=System.currentTimeMillis();
029    
030            private int mode=0777;
031            private int attributes=0;
032            
033            
034            private RamResourceCore parent;
035            
036            /**
037             * Konstruktor
038             * @param parent
039             * @param type
040             * @param name
041             * @param caseSensitive 
042             */
043            public RamResourceCore(RamResourceCore parent, int type,String name) {
044                    if(parent!=null) {
045                            parent.addChild(this);
046                    }
047                    this.parent=parent;
048                    this.type=type;
049                    this.name=name;
050            }
051    
052    
053            /**
054             * Gibt den Feldnamen lastModified zur�ck.
055             * @return lastModified
056             */
057            public long getLastModified() {
058                    return this.lastModified;
059            }
060    
061            /**
062             * Setzt den Feldnamen lastModified.
063             * @param lastModified lastModified 
064             */
065            public void setLastModified(long lastModified) {
066                    this.lastModified = lastModified;
067            }
068    
069            /**
070             * Gibt den Feldnamen children zur�ck.
071             * @return children
072             */
073            public String[] getChildNames() {
074                    if(children==null || children.size()==0) return EMPTY_NAMES;
075                    String[] arr = new String[children.size()];
076                    for(int i=0;i<arr.length;i++) {
077                            arr[i]=((RamResourceCore)children.get(i)).getName();
078                    }
079                    return arr;
080            }
081    
082            /**
083             * Setzt den Feldnamen children.
084             * @param children children 
085             */
086            public void setChildren(List children) {
087                    this.children = children;
088            }
089    
090            /**
091             * Gibt den Feldnamen data zur�ck.
092             * @return data
093             */
094            public byte[] getData() {
095                    return this.data;
096            }
097    
098            /**
099             * Setzt den Feldnamen data.
100             * @param data data 
101             * @param append 
102             */
103            public void setData(byte[] data,boolean append) {
104                    lastModified=System.currentTimeMillis();
105                    
106                    // set data
107                    if(append) {
108                            if(this.data!=null && data!=null) {
109                                    byte[] newData=new byte[this.data.length+data.length];
110                                    int i=0;
111                                    for(;i<this.data.length;i++) {
112                                            newData[i]=this.data[i];
113                                    }
114                                    for(;i<this.data.length+data.length;i++) {
115                                            newData[i]=data[i-this.data.length];
116                                    }
117                                    this.data=newData;
118                            }
119                            else if(data!=null) {
120                                    this.data=data;
121                            }
122                    }
123                    else {
124                            this.data=data;
125                    }
126                    
127                    // set type
128                    if(this.data!=null) this.type=RamResourceCore.TYPE_FILE;
129                    
130            }
131    
132            /**
133             * Gibt den Feldnamen name zur�ck.
134             * @return name
135             */
136            public String getName() {
137                    return this.name;
138            }
139    
140            /**
141             * Setzt den Feldnamen name.
142             * @param name name 
143             */
144            public void setName(String name) {
145                    lastModified=System.currentTimeMillis();
146                    this.name = name;
147            }
148    
149            /**
150             * Gibt den Feldnamen type zur�ck.
151             * @return type
152             */
153            public int getType() {
154                    return this.type;
155            }
156    
157            /**
158             * Setzt den Feldnamen type.
159             * @param type type 
160             */
161            public void setType(int type) {
162                    lastModified=System.currentTimeMillis();
163                    this.type = type;
164            }
165            
166            public void addChild(RamResourceCore child) {
167                    if(children==null)children=new ArrayList();
168                    children.add(child);
169            }
170            
171            /**
172             * returns a child that match given name
173             * @param name
174             * @return matching child
175             */
176            public RamResourceCore getChild(String name, boolean caseSensitive) {
177                    if(children==null) return null;
178                    
179                    RamResourceCore child;
180                    for(int i=children.size()-1;i>=0;i--) {
181                            child=(RamResourceCore) children.get(i);
182                            if(child!=null 
183                                            && (caseSensitive?child.getName().equals(name):child.getName().equalsIgnoreCase(name)))
184                                    return child;
185                    }
186                    return null;
187            }
188    
189            /**
190             * returns the parent if this core
191             * @return parent core or null if no parent available
192             */
193            public RamResourceCore getParent() {
194                    return parent;
195            }
196    
197            /**
198             * remove given child from this core
199             * @param core
200             */
201            public void removeChild(RamResourceCore core) {
202                    
203                    if(children==null) return;
204    
205                    RamResourceCore child;
206                    for(int i=children.size()-1;i>=0;i--) {
207                            child=(RamResourceCore) children.get(i);
208                            if(child==core) {
209                                    children.remove(i);
210                                    break;
211                            }               
212                    }
213            }
214            /**
215             * @return the mode
216             */
217            public int getMode() {
218                    return mode;
219            }
220    
221            /**
222             * @param mode the mode to set
223             */
224            public void setMode(int mode) {
225                    this.mode=mode;
226            }
227            
228            public int getAttributes() {
229                    return attributes;
230            }
231            
232            public void setAttributes(int attributes) {
233                    this.attributes=attributes;
234            }
235    
236            public void remove() {
237                    setType(0);
238                    setData(null,false);
239                    setChildren(null);
240                    RamResourceCore p=getParent();
241                    if(p!=null)p.removeChild(this);
242            }
243    
244    }