KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > spi > IncompleteDeploymentsBuilder


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.deployers.spi;
23
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.jboss.dependency.spi.Controller;
33 import org.jboss.dependency.spi.ControllerContext;
34 import org.jboss.dependency.spi.ControllerState;
35 import org.jboss.dependency.spi.DependencyInfo;
36 import org.jboss.dependency.spi.DependencyItem;
37 import org.jboss.deployers.spi.deployment.MainDeployer;
38 import org.jboss.deployers.spi.structure.DeploymentContext;
39
40 /**
41  * IncompleteDeploymentBuilder.
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 1.1 $
45  */

46 public class IncompleteDeploymentsBuilder
47 {
48    /**
49     * Get the root cause of a throwable
50     *
51     * @param original the original
52     * @return the root
53     */

54    private static Throwable JavaDoc getCause(Throwable JavaDoc original)
55    {
56       if (original == null)
57          return null;
58       Throwable JavaDoc result = original;
59       Throwable JavaDoc cause = result.getCause();
60       while (cause != null)
61       {
62          result = cause;
63          cause = cause.getCause();
64       }
65       return result;
66    }
67    
68    /**
69     * Build an incomplete deployment
70     *
71     * @param main the main deployer
72     * @param controller the controller
73     * @return the incomplte deployments
74     */

75    public static IncompleteDeployments build(MainDeployer main, Controller controller)
76    {
77       Map JavaDoc<String JavaDoc, Throwable JavaDoc> deploymentsInError = null;
78       Collection JavaDoc<String JavaDoc> deploymentsMissingDeployer = null;
79       Map JavaDoc<String JavaDoc, Throwable JavaDoc> contextsInError = null;
80       Map JavaDoc<String JavaDoc, Set JavaDoc<MissingDependency>> contextsMissingDependencies = null;
81       
82       if (main != null)
83       {
84          Collection JavaDoc<DeploymentContext> errors = main.getErrors();
85          if (errors != null && errors.isEmpty() == false)
86          {
87             deploymentsInError = new HashMap JavaDoc<String JavaDoc, Throwable JavaDoc>();
88             for (DeploymentContext context : errors)
89                deploymentsInError.put(context.getName(), getCause(context.getProblem()));
90          }
91          
92          
93          Collection JavaDoc<DeploymentContext> missingDeployer = main.getMissingDeployer();
94          if (missingDeployer != null && missingDeployer.isEmpty() == false)
95          {
96             deploymentsMissingDeployer = new HashSet JavaDoc<String JavaDoc>();
97             for (DeploymentContext context : missingDeployer)
98                deploymentsMissingDeployer.add(context.getName());
99          }
100       }
101       
102       if (controller != null)
103       {
104          List JavaDoc<ControllerState> states = controller.getStates();
105          
106          Set JavaDoc<ControllerContext> notInstalled = controller.getNotInstalled();
107          if (notInstalled.isEmpty() == false)
108          {
109             for (Iterator JavaDoc<ControllerContext> i = notInstalled.iterator(); i.hasNext();)
110             {
111                ControllerContext context = i.next();
112                if (context.getState().equals(context.getRequiredState()))
113                   i.remove();
114             }
115             if (notInstalled.isEmpty() == false)
116             {
117                contextsInError = new HashMap JavaDoc<String JavaDoc, Throwable JavaDoc>();
118                contextsMissingDependencies = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<MissingDependency>>();
119                for (ControllerContext context : notInstalled)
120                {
121                   if (context.getState().equals(ControllerState.ERROR))
122                      contextsInError.put(context.getName().toString(), getCause(context.getError()));
123                   else
124                   {
125                      String JavaDoc name = context.getName().toString();
126                      Set JavaDoc<MissingDependency> dependencies = new HashSet JavaDoc<MissingDependency>();
127                      DependencyInfo dependsInfo = context.getDependencyInfo();
128                      for (DependencyItem item : dependsInfo.getIDependOn(null))
129                      {
130                         if (item.isResolved() == false)
131                         {
132                            String JavaDoc dependency;
133                            ControllerState actualState = null;
134                            String JavaDoc actualStateString;
135                            Object JavaDoc iDependOn = item.getIDependOn();
136                            if (iDependOn == null)
137                            {
138                               dependency = "<UNKNOWN>";
139                               // TODO allow the item to better describe itself
140
actualStateString = "** UNRESOLVED " + item + " **";
141                            }
142                            else
143                            {
144                               dependency = iDependOn.toString();
145                               ControllerContext other = controller.getContext(item.getIDependOn(), null);
146                               if (other == null)
147                                  actualStateString = "** NOT FOUND **";
148                               else
149                               {
150                                  actualState = other.getState();
151                                  actualStateString = actualState.getStateString();
152                               }
153                            }
154                            ControllerState requiredState = item.getWhenRequired();
155                            String JavaDoc requiredStateString = requiredState.getStateString();
156                            int required = states.indexOf(requiredState);
157                            int actual = actualState == null ? -1 : states.indexOf(actualState);
158                            if (required > actual)
159                            {
160                               MissingDependency missing = new MissingDependency(name, dependency, requiredStateString, actualStateString);
161                               dependencies.add(missing);
162                            }
163                         }
164                      }
165                      contextsMissingDependencies.put(name, dependencies);
166                   }
167                }
168             }
169          }
170       }
171       
172       return new IncompleteDeployments(deploymentsInError, deploymentsMissingDeployer, contextsInError, contextsMissingDependencies);
173    }
174 }
175
Popular Tags