001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.naming.resources;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Date;
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.zip.ZipEntry;
029: import java.util.zip.ZipException;
030: import java.util.zip.ZipFile;
031:
032: import javax.naming.CompositeName;
033: import javax.naming.Name;
034: import javax.naming.NamingEnumeration;
035: import javax.naming.NamingException;
036: import javax.naming.OperationNotSupportedException;
037: import javax.naming.directory.Attributes;
038: import javax.naming.directory.DirContext;
039: import javax.naming.directory.ModificationItem;
040: import javax.naming.directory.SearchControls;
041:
042: import org.apache.naming.NamingContextBindingsEnumeration;
043: import org.apache.naming.NamingContextEnumeration;
044: import org.apache.naming.NamingEntry;
045:
046: /**
047: * WAR Directory Context implementation.
048: *
049: * @author Remy Maucherat
050: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
051: */
052:
053: public class WARDirContext extends BaseDirContext {
054:
055: private static org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory
056: .getLog(WARDirContext.class);
057:
058: // ----------------------------------------------------------- Constructors
059:
060: /**
061: * Builds a WAR directory context using the given environment.
062: */
063: public WARDirContext() {
064: super ();
065: }
066:
067: /**
068: * Builds a WAR directory context using the given environment.
069: */
070: public WARDirContext(Hashtable env) {
071: super (env);
072: }
073:
074: /**
075: * Constructor used for returning fake subcontexts.
076: */
077: protected WARDirContext(ZipFile base, Entry entries) {
078: this .base = base;
079: this .entries = entries;
080: }
081:
082: // ----------------------------------------------------- Instance Variables
083:
084: /**
085: * The WAR file.
086: */
087: protected ZipFile base = null;
088:
089: /**
090: * WAR entries.
091: */
092: protected Entry entries = null;
093:
094: // ------------------------------------------------------------- Properties
095:
096: /**
097: * Set the document root.
098: *
099: * @param docBase The new document root
100: *
101: * @exception IllegalArgumentException if the specified value is not
102: * supported by this implementation
103: * @exception IllegalArgumentException if this would create a
104: * malformed URL
105: */
106: public void setDocBase(String docBase) {
107:
108: // Validate the format of the proposed document root
109: if (docBase == null)
110: throw new IllegalArgumentException(sm
111: .getString("resources.null"));
112: if (!(docBase.endsWith(".war")))
113: throw new IllegalArgumentException(sm
114: .getString("warResources.notWar"));
115:
116: // Calculate a File object referencing this document base directory
117: File base = new File(docBase);
118:
119: // Validate that the document base is an existing directory
120: if (!base.exists() || !base.canRead() || base.isDirectory())
121: throw new IllegalArgumentException(sm.getString(
122: "warResources.invalidWar", docBase));
123: try {
124: this .base = new ZipFile(base);
125: } catch (Exception e) {
126: throw new IllegalArgumentException(sm.getString(
127: "warResources.invalidWar", e.getMessage()));
128: }
129: super .setDocBase(docBase);
130:
131: loadEntries();
132:
133: }
134:
135: // --------------------------------------------------------- Public Methods
136:
137: /**
138: * Release any resources allocated for this directory context.
139: */
140: public void release() {
141:
142: entries = null;
143: if (base != null) {
144: try {
145: base.close();
146: } catch (IOException e) {
147: log.warn(
148: "Exception closing WAR File " + base.getName(),
149: e);
150: }
151: }
152: base = null;
153: super .release();
154:
155: }
156:
157: // -------------------------------------------------------- Context Methods
158:
159: /**
160: * Retrieves the named object.
161: *
162: * @param name the name of the object to look up
163: * @return the object bound to name
164: * @exception NamingException if a naming exception is encountered
165: */
166: public Object lookup(String name) throws NamingException {
167: return lookup(new CompositeName(name));
168: }
169:
170: /**
171: * Retrieves the named object. If name is empty, returns a new instance
172: * of this context (which represents the same naming context as this
173: * context, but its environment may be modified independently and it may
174: * be accessed concurrently).
175: *
176: * @param name the name of the object to look up
177: * @return the object bound to name
178: * @exception NamingException if a naming exception is encountered
179: */
180: public Object lookup(Name name) throws NamingException {
181: if (name.isEmpty())
182: return this ;
183: Entry entry = treeLookup(name);
184: if (entry == null)
185: throw new NamingException(sm.getString(
186: "resources.notFound", name));
187: ZipEntry zipEntry = entry.getEntry();
188: if (zipEntry.isDirectory())
189: return new WARDirContext(base, entry);
190: else
191: return new WARResource(entry.getEntry());
192: }
193:
194: /**
195: * Unbinds the named object. Removes the terminal atomic name in name
196: * from the target context--that named by all but the terminal atomic
197: * part of name.
198: * <p>
199: * This method is idempotent. It succeeds even if the terminal atomic
200: * name is not bound in the target context, but throws
201: * NameNotFoundException if any of the intermediate contexts do not exist.
202: *
203: * @param name the name to bind; may not be empty
204: * @exception NameNotFoundException if an intermediate context does not
205: * exist
206: * @exception NamingException if a naming exception is encountered
207: */
208: public void unbind(String name) throws NamingException {
209: throw new OperationNotSupportedException();
210: }
211:
212: /**
213: * Binds a new name to the object bound to an old name, and unbinds the
214: * old name. Both names are relative to this context. Any attributes
215: * associated with the old name become associated with the new name.
216: * Intermediate contexts of the old name are not changed.
217: *
218: * @param oldName the name of the existing binding; may not be empty
219: * @param newName the name of the new binding; may not be empty
220: * @exception NameAlreadyBoundException if newName is already bound
221: * @exception NamingException if a naming exception is encountered
222: */
223: public void rename(String oldName, String newName)
224: throws NamingException {
225: throw new OperationNotSupportedException();
226: }
227:
228: /**
229: * Enumerates the names bound in the named context, along with the class
230: * names of objects bound to them. The contents of any subcontexts are
231: * not included.
232: * <p>
233: * If a binding is added to or removed from this context, its effect on
234: * an enumeration previously returned is undefined.
235: *
236: * @param name the name of the context to list
237: * @return an enumeration of the names and class names of the bindings in
238: * this context. Each element of the enumeration is of type NameClassPair.
239: * @exception NamingException if a naming exception is encountered
240: */
241: public NamingEnumeration list(String name) throws NamingException {
242: return list(new CompositeName(name));
243: }
244:
245: /**
246: * Enumerates the names bound in the named context, along with the class
247: * names of objects bound to them. The contents of any subcontexts are
248: * not included.
249: * <p>
250: * If a binding is added to or removed from this context, its effect on
251: * an enumeration previously returned is undefined.
252: *
253: * @param name the name of the context to list
254: * @return an enumeration of the names and class names of the bindings in
255: * this context. Each element of the enumeration is of type NameClassPair.
256: * @exception NamingException if a naming exception is encountered
257: */
258: public NamingEnumeration list(Name name) throws NamingException {
259: if (name.isEmpty())
260: return new NamingContextEnumeration(list(entries)
261: .iterator());
262: Entry entry = treeLookup(name);
263: if (entry == null)
264: throw new NamingException(sm.getString(
265: "resources.notFound", name));
266: return new NamingContextEnumeration(list(entry).iterator());
267: }
268:
269: /**
270: * Enumerates the names bound in the named context, along with the
271: * objects bound to them. The contents of any subcontexts are not
272: * included.
273: * <p>
274: * If a binding is added to or removed from this context, its effect on
275: * an enumeration previously returned is undefined.
276: *
277: * @param name the name of the context to list
278: * @return an enumeration of the bindings in this context.
279: * Each element of the enumeration is of type Binding.
280: * @exception NamingException if a naming exception is encountered
281: */
282: public NamingEnumeration listBindings(String name)
283: throws NamingException {
284: return listBindings(new CompositeName(name));
285: }
286:
287: /**
288: * Enumerates the names bound in the named context, along with the
289: * objects bound to them. The contents of any subcontexts are not
290: * included.
291: * <p>
292: * If a binding is added to or removed from this context, its effect on
293: * an enumeration previously returned is undefined.
294: *
295: * @param name the name of the context to list
296: * @return an enumeration of the bindings in this context.
297: * Each element of the enumeration is of type Binding.
298: * @exception NamingException if a naming exception is encountered
299: */
300: public NamingEnumeration listBindings(Name name)
301: throws NamingException {
302: if (name.isEmpty())
303: return new NamingContextBindingsEnumeration(list(entries)
304: .iterator(), this );
305: Entry entry = treeLookup(name);
306: if (entry == null)
307: throw new NamingException(sm.getString(
308: "resources.notFound", name));
309: return new NamingContextBindingsEnumeration(list(entry)
310: .iterator(), this );
311: }
312:
313: /**
314: * Destroys the named context and removes it from the namespace. Any
315: * attributes associated with the name are also removed. Intermediate
316: * contexts are not destroyed.
317: * <p>
318: * This method is idempotent. It succeeds even if the terminal atomic
319: * name is not bound in the target context, but throws
320: * NameNotFoundException if any of the intermediate contexts do not exist.
321: *
322: * In a federated naming system, a context from one naming system may be
323: * bound to a name in another. One can subsequently look up and perform
324: * operations on the foreign context using a composite name. However, an
325: * attempt destroy the context using this composite name will fail with
326: * NotContextException, because the foreign context is not a "subcontext"
327: * of the context in which it is bound. Instead, use unbind() to remove
328: * the binding of the foreign context. Destroying the foreign context
329: * requires that the destroySubcontext() be performed on a context from
330: * the foreign context's "native" naming system.
331: *
332: * @param name the name of the context to be destroyed; may not be empty
333: * @exception NameNotFoundException if an intermediate context does not
334: * exist
335: * @exception NotContextException if the name is bound but does not name
336: * a context, or does not name a context of the appropriate type
337: */
338: public void destroySubcontext(String name) throws NamingException {
339: throw new OperationNotSupportedException();
340: }
341:
342: /**
343: * Retrieves the named object, following links except for the terminal
344: * atomic component of the name. If the object bound to name is not a
345: * link, returns the object itself.
346: *
347: * @param name the name of the object to look up
348: * @return the object bound to name, not following the terminal link
349: * (if any).
350: * @exception NamingException if a naming exception is encountered
351: */
352: public Object lookupLink(String name) throws NamingException {
353: // Note : Links are not supported
354: return lookup(name);
355: }
356:
357: /**
358: * Retrieves the full name of this context within its own namespace.
359: * <p>
360: * Many naming services have a notion of a "full name" for objects in
361: * their respective namespaces. For example, an LDAP entry has a
362: * distinguished name, and a DNS record has a fully qualified name. This
363: * method allows the client application to retrieve this name. The string
364: * returned by this method is not a JNDI composite name and should not be
365: * passed directly to context methods. In naming systems for which the
366: * notion of full name does not make sense,
367: * OperationNotSupportedException is thrown.
368: *
369: * @return this context's name in its own namespace; never null
370: * @exception OperationNotSupportedException if the naming system does
371: * not have the notion of a full name
372: * @exception NamingException if a naming exception is encountered
373: */
374: public String getNameInNamespace() throws NamingException {
375: return docBase;
376: }
377:
378: // ----------------------------------------------------- DirContext Methods
379:
380: /**
381: * Retrieves selected attributes associated with a named object.
382: * See the class description regarding attribute models, attribute type
383: * names, and operational attributes.
384: *
385: * @return the requested attributes; never null
386: * @param name the name of the object from which to retrieve attributes
387: * @param attrIds the identifiers of the attributes to retrieve. null
388: * indicates that all attributes should be retrieved; an empty array
389: * indicates that none should be retrieved
390: * @exception NamingException if a naming exception is encountered
391: */
392: public Attributes getAttributes(String name, String[] attrIds)
393: throws NamingException {
394: return getAttributes(new CompositeName(name), attrIds);
395: }
396:
397: /**
398: * Retrieves all of the attributes associated with a named object.
399: *
400: * @return the set of attributes associated with name.
401: * Returns an empty attribute set if name has no attributes; never null.
402: * @param name the name of the object from which to retrieve attributes
403: * @exception NamingException if a naming exception is encountered
404: */
405: public Attributes getAttributes(Name name, String[] attrIds)
406: throws NamingException {
407:
408: Entry entry = null;
409: if (name.isEmpty())
410: entry = entries;
411: else
412: entry = treeLookup(name);
413: if (entry == null)
414: throw new NamingException(sm.getString(
415: "resources.notFound", name));
416:
417: ZipEntry zipEntry = entry.getEntry();
418:
419: ResourceAttributes attrs = new ResourceAttributes();
420: attrs.setCreationDate(new Date(zipEntry.getTime()));
421: attrs.setName(entry.getName());
422: if (!zipEntry.isDirectory())
423: attrs.setResourceType("");
424: attrs.setContentLength(zipEntry.getSize());
425: attrs.setLastModified(zipEntry.getTime());
426:
427: return attrs;
428:
429: }
430:
431: /**
432: * Modifies the attributes associated with a named object. The order of
433: * the modifications is not specified. Where possible, the modifications
434: * are performed atomically.
435: *
436: * @param name the name of the object whose attributes will be updated
437: * @param mod_op the modification operation, one of: ADD_ATTRIBUTE,
438: * REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE
439: * @param attrs the attributes to be used for the modification; may not
440: * be null
441: * @exception AttributeModificationException if the modification cannot be
442: * completed successfully
443: * @exception NamingException if a naming exception is encountered
444: */
445: public void modifyAttributes(String name, int mod_op,
446: Attributes attrs) throws NamingException {
447: throw new OperationNotSupportedException();
448: }
449:
450: /**
451: * Modifies the attributes associated with a named object using an an
452: * ordered list of modifications. The modifications are performed in the
453: * order specified. Each modification specifies a modification operation
454: * code and an attribute on which to operate. Where possible, the
455: * modifications are performed atomically.
456: *
457: * @param name the name of the object whose attributes will be updated
458: * @param mods an ordered sequence of modifications to be performed; may
459: * not be null
460: * @exception AttributeModificationException if the modification cannot be
461: * completed successfully
462: * @exception NamingException if a naming exception is encountered
463: */
464: public void modifyAttributes(String name, ModificationItem[] mods)
465: throws NamingException {
466: throw new OperationNotSupportedException();
467: }
468:
469: /**
470: * Binds a name to an object, along with associated attributes. If attrs
471: * is null, the resulting binding will have the attributes associated
472: * with obj if obj is a DirContext, and no attributes otherwise. If attrs
473: * is non-null, the resulting binding will have attrs as its attributes;
474: * any attributes associated with obj are ignored.
475: *
476: * @param name the name to bind; may not be empty
477: * @param obj the object to bind; possibly null
478: * @param attrs the attributes to associate with the binding
479: * @exception NameAlreadyBoundException if name is already bound
480: * @exception InvalidAttributesException if some "mandatory" attributes
481: * of the binding are not supplied
482: * @exception NamingException if a naming exception is encountered
483: */
484: public void bind(String name, Object obj, Attributes attrs)
485: throws NamingException {
486: throw new OperationNotSupportedException();
487: }
488:
489: /**
490: * Binds a name to an object, along with associated attributes,
491: * overwriting any existing binding. If attrs is null and obj is a
492: * DirContext, the attributes from obj are used. If attrs is null and obj
493: * is not a DirContext, any existing attributes associated with the object
494: * already bound in the directory remain unchanged. If attrs is non-null,
495: * any existing attributes associated with the object already bound in
496: * the directory are removed and attrs is associated with the named
497: * object. If obj is a DirContext and attrs is non-null, the attributes
498: * of obj are ignored.
499: *
500: * @param name the name to bind; may not be empty
501: * @param obj the object to bind; possibly null
502: * @param attrs the attributes to associate with the binding
503: * @exception InvalidAttributesException if some "mandatory" attributes
504: * of the binding are not supplied
505: * @exception NamingException if a naming exception is encountered
506: */
507: public void rebind(String name, Object obj, Attributes attrs)
508: throws NamingException {
509: throw new OperationNotSupportedException();
510: }
511:
512: /**
513: * Creates and binds a new context, along with associated attributes.
514: * This method creates a new subcontext with the given name, binds it in
515: * the target context (that named by all but terminal atomic component of
516: * the name), and associates the supplied attributes with the newly
517: * created object. All intermediate and target contexts must already
518: * exist. If attrs is null, this method is equivalent to
519: * Context.createSubcontext().
520: *
521: * @param name the name of the context to create; may not be empty
522: * @param attrs the attributes to associate with the newly created context
523: * @return the newly created context
524: * @exception NameAlreadyBoundException if the name is already bound
525: * @exception InvalidAttributesException if attrs does not contain all
526: * the mandatory attributes required for creation
527: * @exception NamingException if a naming exception is encountered
528: */
529: public DirContext createSubcontext(String name, Attributes attrs)
530: throws NamingException {
531: throw new OperationNotSupportedException();
532: }
533:
534: /**
535: * Retrieves the schema associated with the named object. The schema
536: * describes rules regarding the structure of the namespace and the
537: * attributes stored within it. The schema specifies what types of
538: * objects can be added to the directory and where they can be added;
539: * what mandatory and optional attributes an object can have. The range
540: * of support for schemas is directory-specific.
541: *
542: * @param name the name of the object whose schema is to be retrieved
543: * @return the schema associated with the context; never null
544: * @exception OperationNotSupportedException if schema not supported
545: * @exception NamingException if a naming exception is encountered
546: */
547: public DirContext getSchema(String name) throws NamingException {
548: throw new OperationNotSupportedException();
549: }
550:
551: /**
552: * Retrieves a context containing the schema objects of the named
553: * object's class definitions.
554: *
555: * @param name the name of the object whose object class definition is to
556: * be retrieved
557: * @return the DirContext containing the named object's class
558: * definitions; never null
559: * @exception OperationNotSupportedException if schema not supported
560: * @exception NamingException if a naming exception is encountered
561: */
562: public DirContext getSchemaClassDefinition(String name)
563: throws NamingException {
564: throw new OperationNotSupportedException();
565: }
566:
567: /**
568: * Searches in a single context for objects that contain a specified set
569: * of attributes, and retrieves selected attributes. The search is
570: * performed using the default SearchControls settings.
571: *
572: * @param name the name of the context to search
573: * @param matchingAttributes the attributes to search for. If empty or
574: * null, all objects in the target context are returned.
575: * @param attributesToReturn the attributes to return. null indicates
576: * that all attributes are to be returned; an empty array indicates that
577: * none are to be returned.
578: * @return a non-null enumeration of SearchResult objects. Each
579: * SearchResult contains the attributes identified by attributesToReturn
580: * and the name of the corresponding object, named relative to the
581: * context named by name.
582: * @exception NamingException if a naming exception is encountered
583: */
584: public NamingEnumeration search(String name,
585: Attributes matchingAttributes, String[] attributesToReturn)
586: throws NamingException {
587: throw new OperationNotSupportedException();
588: }
589:
590: /**
591: * Searches in a single context for objects that contain a specified set
592: * of attributes. This method returns all the attributes of such objects.
593: * It is equivalent to supplying null as the atributesToReturn parameter
594: * to the method search(Name, Attributes, String[]).
595: *
596: * @param name the name of the context to search
597: * @param matchingAttributes the attributes to search for. If empty or
598: * null, all objects in the target context are returned.
599: * @return a non-null enumeration of SearchResult objects. Each
600: * SearchResult contains the attributes identified by attributesToReturn
601: * and the name of the corresponding object, named relative to the
602: * context named by name.
603: * @exception NamingException if a naming exception is encountered
604: */
605: public NamingEnumeration search(String name,
606: Attributes matchingAttributes) throws NamingException {
607: throw new OperationNotSupportedException();
608: }
609:
610: /**
611: * Searches in the named context or object for entries that satisfy the
612: * given search filter. Performs the search as specified by the search
613: * controls.
614: *
615: * @param name the name of the context or object to search
616: * @param filter the filter expression to use for the search; may not be
617: * null
618: * @param cons the search controls that control the search. If null,
619: * the default search controls are used (equivalent to
620: * (new SearchControls())).
621: * @return an enumeration of SearchResults of the objects that satisfy
622: * the filter; never null
623: * @exception InvalidSearchFilterException if the search filter specified
624: * is not supported or understood by the underlying directory
625: * @exception InvalidSearchControlsException if the search controls
626: * contain invalid settings
627: * @exception NamingException if a naming exception is encountered
628: */
629: public NamingEnumeration search(String name, String filter,
630: SearchControls cons) throws NamingException {
631: throw new OperationNotSupportedException();
632: }
633:
634: /**
635: * Searches in the named context or object for entries that satisfy the
636: * given search filter. Performs the search as specified by the search
637: * controls.
638: *
639: * @param name the name of the context or object to search
640: * @param filterExpr the filter expression to use for the search.
641: * The expression may contain variables of the form "{i}" where i is a
642: * nonnegative integer. May not be null.
643: * @param filterArgs the array of arguments to substitute for the
644: * variables in filterExpr. The value of filterArgs[i] will replace each
645: * occurrence of "{i}". If null, equivalent to an empty array.
646: * @param cons the search controls that control the search. If null, the
647: * default search controls are used (equivalent to (new SearchControls())).
648: * @return an enumeration of SearchResults of the objects that satisy the
649: * filter; never null
650: * @exception ArrayIndexOutOfBoundsException if filterExpr contains {i}
651: * expressions where i is outside the bounds of the array filterArgs
652: * @exception InvalidSearchControlsException if cons contains invalid
653: * settings
654: * @exception InvalidSearchFilterException if filterExpr with filterArgs
655: * represents an invalid search filter
656: * @exception NamingException if a naming exception is encountered
657: */
658: public NamingEnumeration search(String name, String filterExpr,
659: Object[] filterArgs, SearchControls cons)
660: throws NamingException {
661: throw new OperationNotSupportedException();
662: }
663:
664: // ------------------------------------------------------ Protected Methods
665:
666: /**
667: * Normalize the name of an entry read from the Zip.
668: */
669: protected String normalize(ZipEntry entry) {
670:
671: String result = "/" + entry.getName();
672: if (entry.isDirectory()) {
673: result = result.substring(0, result.length() - 1);
674: }
675: return result;
676:
677: }
678:
679: /**
680: * Constructs a tree of the entries contained in a WAR file.
681: */
682: protected void loadEntries() {
683:
684: try {
685:
686: Enumeration entryList = base.entries();
687: entries = new Entry("/", new ZipEntry("/"));
688:
689: while (entryList.hasMoreElements()) {
690:
691: ZipEntry entry = (ZipEntry) entryList.nextElement();
692: String name = normalize(entry);
693: int pos = name.lastIndexOf('/');
694: // Check that parent entries exist and, if not, create them.
695: // This fixes a bug for war files that don't record separate
696: // zip entries for the directories.
697: int currentPos = -1;
698: int lastPos = 0;
699: while ((currentPos = name.indexOf('/', lastPos)) != -1) {
700: Name parentName = new CompositeName(name.substring(
701: 0, lastPos));
702: Name childName = new CompositeName(name.substring(
703: 0, currentPos));
704: String entryName = name.substring(lastPos,
705: currentPos);
706: // Parent should have been created in last cycle through
707: // this loop
708: Entry parent = treeLookup(parentName);
709: Entry child = treeLookup(childName);
710: if (child == null) {
711: // Create a new entry for missing entry and strip off
712: // the leading '/' character and appended on by the
713: // normalize method and add '/' character to end to
714: // signify that it is a directory entry
715: String zipName = name.substring(1, currentPos)
716: + "/";
717: child = new Entry(entryName, new ZipEntry(
718: zipName));
719: if (parent != null)
720: parent.addChild(child);
721: }
722: // Increment lastPos
723: lastPos = currentPos + 1;
724: }
725: String entryName = name.substring(pos + 1, name
726: .length());
727: Name compositeName = new CompositeName(name.substring(
728: 0, pos));
729: Entry parent = treeLookup(compositeName);
730: Entry child = new Entry(entryName, entry);
731: if (parent != null)
732: parent.addChild(child);
733:
734: }
735:
736: } catch (Exception e) {
737: }
738:
739: }
740:
741: /**
742: * Entry tree lookup.
743: */
744: protected Entry treeLookup(Name name) {
745: if (name.isEmpty())
746: return entries;
747: Entry currentEntry = entries;
748: for (int i = 0; i < name.size(); i++) {
749: if (name.get(i).length() == 0)
750: continue;
751: currentEntry = currentEntry.getChild(name.get(i));
752: if (currentEntry == null)
753: return null;
754: }
755: return currentEntry;
756: }
757:
758: /**
759: * List children as objects.
760: */
761: protected ArrayList list(Entry entry) {
762:
763: ArrayList entries = new ArrayList();
764: Entry[] children = entry.getChildren();
765: Arrays.sort(children);
766: NamingEntry namingEntry = null;
767:
768: for (int i = 0; i < children.length; i++) {
769: ZipEntry current = children[i].getEntry();
770: Object object = null;
771: if (current.isDirectory()) {
772: object = new WARDirContext(base, children[i]);
773: } else {
774: object = new WARResource(current);
775: }
776: namingEntry = new NamingEntry(children[i].getName(),
777: object, NamingEntry.ENTRY);
778: entries.add(namingEntry);
779: }
780:
781: return entries;
782:
783: }
784:
785: // ---------------------------------------------------- Entries Inner Class
786:
787: /**
788: * Entries structure.
789: */
790: protected class Entry implements Comparable {
791:
792: // -------------------------------------------------------- Constructor
793:
794: public Entry(String name, ZipEntry entry) {
795: this .name = name;
796: this .entry = entry;
797: }
798:
799: // --------------------------------------------------- Member Variables
800:
801: protected String name = null;
802:
803: protected ZipEntry entry = null;
804:
805: protected Entry children[] = new Entry[0];
806:
807: // ----------------------------------------------------- Public Methods
808:
809: public int compareTo(Object o) {
810: if (!(o instanceof Entry))
811: return (+1);
812: return (name.compareTo(((Entry) o).getName()));
813: }
814:
815: public ZipEntry getEntry() {
816: return entry;
817: }
818:
819: public String getName() {
820: return name;
821: }
822:
823: public void addChild(Entry entry) {
824: Entry[] newChildren = new Entry[children.length + 1];
825: for (int i = 0; i < children.length; i++)
826: newChildren[i] = children[i];
827: newChildren[children.length] = entry;
828: children = newChildren;
829: }
830:
831: public Entry[] getChildren() {
832: return children;
833: }
834:
835: public Entry getChild(String name) {
836: for (int i = 0; i < children.length; i++) {
837: if (children[i].name.equals(name)) {
838: return children[i];
839: }
840: }
841: return null;
842: }
843:
844: }
845:
846: // ------------------------------------------------ WARResource Inner Class
847:
848: /**
849: * This specialized resource implementation avoids opening the IputStream
850: * to the WAR right away.
851: */
852: protected class WARResource extends Resource {
853:
854: // -------------------------------------------------------- Constructor
855:
856: public WARResource(ZipEntry entry) {
857: this .entry = entry;
858: }
859:
860: // --------------------------------------------------- Member Variables
861:
862: protected ZipEntry entry;
863:
864: // ----------------------------------------------------- Public Methods
865:
866: /**
867: * Content accessor.
868: *
869: * @return InputStream
870: */
871: public InputStream streamContent() throws IOException {
872: try {
873: if (binaryContent == null) {
874: inputStream = base.getInputStream(entry);
875: }
876: } catch (ZipException e) {
877: throw new IOException(e.getMessage());
878: }
879: return super.streamContent();
880: }
881:
882: }
883:
884: }
|