KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > structure > StructureMetaDataImpl


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployers.plugins.structure;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import org.jboss.deployers.spi.structure.vfs.ContextInfo;
31 import org.jboss.deployers.spi.structure.vfs.StructureMetaData;
32
33 /**
34  * Metadata describing the structure of a deployment.
35  *
36  * @author Scott.Stark@jboss.org
37  * @version $Revision:$
38  */

39 public class StructureMetaDataImpl
40    implements StructureMetaData, Serializable JavaDoc
41 {
42    private static final long serialVersionUID = 1;
43
44    private HashMap JavaDoc<String JavaDoc, ContextInfo> contextMap =
45       new HashMap JavaDoc<String JavaDoc, ContextInfo>();
46    private TreeSet JavaDoc<ContextInfo> contextSet = new TreeSet JavaDoc<ContextInfo>(new ContextComparator());
47
48    public void addContext(ContextInfo context)
49    {
50       String JavaDoc key = context.getVfsPath();
51       contextMap.put(key, context);
52       ContextInfo parent = context.getParent();
53       // If the parent has not been set try to find it
54
if( parent == null )
55       {
56          String JavaDoc[] keys = key.split("/");
57          StringBuilder JavaDoc parentKey = new StringBuilder JavaDoc();
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          // Handle a VFS rooted at ""
68
if( keys.length == 1 )
69          {
70             // Look for "" as the parent
71
parent = contextMap.get("");
72             if( parent != null )
73                context.setParent(parent);
74          }
75       }
76       contextSet.add(context);
77    }
78
79    public ContextInfo getContext(String JavaDoc vfsPath)
80    {
81       return contextMap.get(vfsPath);
82    }
83
84    public ContextInfo removeContext(String JavaDoc vfsPath)
85    {
86       ContextInfo info = contextMap.remove(vfsPath);
87       if( info != null )
88          contextSet.remove(info);
89       return info;
90    }
91
92    public SortedSet JavaDoc<ContextInfo> getContexts()
93    {
94       return contextSet;
95    }
96
97    public String JavaDoc toString()
98    {
99       StringBuilder JavaDoc tmp = new StringBuilder JavaDoc(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 JavaDoc<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             // Sort by depth and then name
118
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