1 22 package org.jboss.deployers.plugins.structure; 23 24 import java.io.Serializable ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.SortedSet ; 28 import java.util.TreeSet ; 29 30 import org.jboss.deployers.spi.structure.vfs.ContextInfo; 31 import org.jboss.deployers.spi.structure.vfs.StructureMetaData; 32 33 39 public class StructureMetaDataImpl 40 implements StructureMetaData, Serializable 41 { 42 private static final long serialVersionUID = 1; 43 44 private HashMap <String , ContextInfo> contextMap = 45 new HashMap <String , ContextInfo>(); 46 private TreeSet <ContextInfo> contextSet = new TreeSet <ContextInfo>(new ContextComparator()); 47 48 public void addContext(ContextInfo context) 49 { 50 String key = context.getVfsPath(); 51 contextMap.put(key, context); 52 ContextInfo parent = context.getParent(); 53 if( parent == null ) 55 { 56 String [] keys = key.split("/"); 57 StringBuilder parentKey = new StringBuilder (); 58 for(int n = 0; n < keys.length-1; n ++) 59 { 60 key = keys[n]; 61 parentKey.append(key); 62 parent = contextMap.get(parentKey.toString()); 63 if( parent != null ) 64 context.setParent(parent); 65 parentKey.append('/'); 66 } 67 if( keys.length == 1 ) 69 { 70 parent = contextMap.get(""); 72 if( parent != null ) 73 context.setParent(parent); 74 } 75 } 76 contextSet.add(context); 77 } 78 79 public ContextInfo getContext(String vfsPath) 80 { 81 return contextMap.get(vfsPath); 82 } 83 84 public ContextInfo removeContext(String vfsPath) 85 { 86 ContextInfo info = contextMap.remove(vfsPath); 87 if( info != null ) 88 contextSet.remove(info); 89 return info; 90 } 91 92 public SortedSet <ContextInfo> getContexts() 93 { 94 return contextSet; 95 } 96 97 public String toString() 98 { 99 StringBuilder tmp = new StringBuilder (super.toString()); 100 tmp.append("[ContextInfo:"); 101 tmp.append(contextSet.toString()); 102 tmp.append(']'); 103 return tmp.toString(); 104 } 105 106 private class ContextComparator implements Comparator <ContextInfo> 107 { 108 public int compare(ContextInfo o1, ContextInfo o2) 109 { 110 int compare = 0; 111 if( o1 == null && o2 != null ) 112 compare = -1; 113 else if( o1 != null && o2 == null ) 114 compare = 1; 115 else 116 { 117 ContextInfo p1 = o1.getParent(); 119 ContextInfo p2 = o2.getParent(); 120 if( p1 != p2 ) 121 { 122 compare = compare(p1, p2); 123 } 124 else if( p1 != null ) 125 { 126 compare = o1.getVfsPath().compareTo(o2.getVfsPath()); 127 } 128 } 129 return compare; 130 } 131 } 132 } 133 | Popular Tags |