KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > dependency > plugins > AbstractDependencyInfo


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
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.dependency.plugins;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.jboss.dependency.spi.Controller;
29 import org.jboss.dependency.spi.ControllerState;
30 import org.jboss.dependency.spi.DependencyInfo;
31 import org.jboss.dependency.spi.DependencyItem;
32 import org.jboss.util.JBossObject;
33 import org.jboss.util.JBossStringBuilder;
34 import org.jboss.util.collection.CollectionsFactory;
35
36 /**
37  * A DependencyInfo.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 45764 $
41  */

42 public class AbstractDependencyInfo extends JBossObject implements DependencyInfo
43 {
44    /** My dependencies */
45    private Set JavaDoc<DependencyItem> iDependOn = CollectionsFactory.createCopyOnWriteSet();
46
47    /** Dependencies referencing me */
48    private Set JavaDoc<DependencyItem> dependsOnMe = CollectionsFactory.createCopyOnWriteSet();
49
50    /** Unresolved dependencies */
51    private Set JavaDoc<DependencyItem> unresolved = CollectionsFactory.createCopyOnWriteSet();
52
53    /**
54     * Create an abstract dependency info
55     */

56    public AbstractDependencyInfo()
57    {
58    }
59
60    public Set JavaDoc<DependencyItem> getIDependOn(Class JavaDoc type)
61    {
62       if (type == null || iDependOn.isEmpty())
63          return iDependOn;
64       else
65       {
66          HashSet JavaDoc<DependencyItem> set = new HashSet JavaDoc<DependencyItem>();
67          for (Iterator JavaDoc i = iDependOn.iterator(); i.hasNext();)
68          {
69             DependencyItem item = (DependencyItem) i.next();
70             if (type.isInstance(item))
71                set.add(item);
72          }
73          return set;
74       }
75    }
76    
77    public void addIDependOn(DependencyItem dependency)
78    {
79       iDependOn.add(dependency);
80       unresolved.add(dependency);
81       flushJBossObjectCache();
82    }
83
84    public void removeIDependOn(DependencyItem dependency)
85    {
86       iDependOn.remove(dependency);
87       unresolved.remove(dependency);
88       flushJBossObjectCache();
89    }
90    
91    public Set JavaDoc<DependencyItem> getDependsOnMe(Class JavaDoc type)
92    {
93       if (type == null || dependsOnMe.isEmpty())
94          return dependsOnMe;
95       else
96       {
97          HashSet JavaDoc<DependencyItem> set = new HashSet JavaDoc<DependencyItem>();
98          for (Iterator JavaDoc i = dependsOnMe.iterator(); i.hasNext();)
99          {
100             DependencyItem item = (DependencyItem) i.next();
101             if (type.isInstance(item))
102                set.add(item);
103          }
104          return set;
105       }
106    }
107    
108    public void addDependsOnMe(DependencyItem dependency)
109    {
110       dependsOnMe.add(dependency);
111       flushJBossObjectCache();
112    }
113
114    public void removeDependsOnMe(DependencyItem dependency)
115    {
116       dependsOnMe.add(dependency);
117       flushJBossObjectCache();
118    }
119    
120    public boolean resolveDependencies(Controller controller, ControllerState state)
121    {
122       boolean resolved = true;
123       if (unresolved.isEmpty() == false)
124       {
125          for (Iterator JavaDoc i = unresolved.iterator(); i.hasNext();)
126          {
127             DependencyItem item = (DependencyItem) i.next();
128             if (state.equals(item.getWhenRequired()) && item.resolve(controller) == false)
129             {
130                resolved = false;
131                break;
132             }
133          }
134       }
135       return resolved;
136    }
137
138    public Set JavaDoc<DependencyItem> getUnresolvedDependencies()
139    {
140       return unresolved;
141    }
142    
143    public void toString(JBossStringBuilder buffer)
144    {
145       buffer.append("idependOn=").append(iDependOn);
146       if (unresolved.isEmpty() == false)
147          buffer.append(" unresolved=").append(unresolved);
148    }
149 }
150
Popular Tags