KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > server > lib > ClifServerImpl


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.server.lib;
25
26 import org.objectweb.clif.server.api.ClifServerControl;
27 import org.objectweb.clif.supervisor.api.SupervisorInfo;
28 import org.objectweb.clif.supervisor.api.ClifException;
29 import org.objectweb.clif.util.ClifClassLoader;
30 import org.objectweb.fractal.api.Component;
31 import org.objectweb.fractal.api.NoSuchInterfaceException;
32 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
33 import org.objectweb.fractal.api.control.IllegalContentException;
34 import org.objectweb.fractal.util.Fractal;
35 import org.objectweb.fractal.adl.FactoryFactory;
36 import org.objectweb.fractal.adl.Factory;
37 import org.objectweb.fractal.adl.ADLException;
38 import org.objectweb.util.monolog.api.Logger;
39 import org.objectweb.util.monolog.api.BasicLevel;
40 import org.objectweb.util.monolog.Monolog;
41 import java.util.Map JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44
45 /**
46  * Manages blade creation and removal within a given Clif server.
47  * @author Bruno Dillenseger
48  */

49 public class ClifServerImpl
50     implements ClifServerControl
51 {
52     static protected Logger log = Monolog.getDefaultMonologFactory().getLogger(ClifServerImpl.class.toString());
53
54
55     /** contains blade components of this CLIF server, indexed by their identifier */
56     protected Map JavaDoc blades = new HashMap JavaDoc();
57
58
59     public ClifServerImpl()
60     {
61     }
62
63
64     /////////////////////////////////
65
// interface ClifServerControl //
66
/////////////////////////////////
67

68
69     /**
70      * Creates a new blade component managed by this Clif server from an ADL definition.
71      * @param adlDefinition ADL definition name of a blade implementation
72      * @param context context that will be used for blade instantiation
73      * @param bladeId identifier for the new blade
74      * @return the BladeControl interface of the new Blade component
75      * @throws ClifException
76      */

77     public synchronized Component addBlade(Component clifApp, String JavaDoc adlDefinition, Map JavaDoc context, String JavaDoc bladeId)
78         throws ClifException
79     {
80         try
81         {
82             Factory adlFactory = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
83             context.put("classloader", ClifClassLoader.getClassLoader());
84             Component blade = (Component)adlFactory.newComponent(adlDefinition, context);
85             Fractal.getContentController(clifApp).addFcSubComponent(blade);
86             blades.put(bladeId, blade);
87             return blade;
88         }
89         catch (ADLException ex)
90         {
91             String JavaDoc message = "Unable to deploy blade " + bladeId;
92             log.log(BasicLevel.ERROR, message, ex);
93             throw new ClifException(message, ex);
94         }
95         catch (NoSuchInterfaceException ex)
96         {
97             throw new Error JavaDoc(
98                 "Bad configuration: unable to get Clif Application content controller",
99                 ex);
100         }
101         catch (IllegalContentException ex)
102         {
103             throw new Error JavaDoc(
104                 "Bad configuration: unable to add a " + adlDefinition + " blade to Clif Application",
105                 ex);
106         }
107         catch (IllegalLifeCycleException ex)
108         {
109             throw new Error JavaDoc("Unable to add a new blade to the Clif Application", ex);
110         }
111     }
112
113
114     /**
115      * Unbinds and removes every blade managed by this CLIF server.
116      */

117     public synchronized void removeAllBlades(Component clifApp)
118         throws ClifException
119     {
120         String JavaDoc[] ids = (String JavaDoc[])blades.keySet().toArray(new String JavaDoc[blades.size()]);
121         for (int i=0 ; i<ids.length ; ++i)
122         {
123             removeBlade(clifApp, ids[i]);
124         }
125     }
126
127
128     /**
129      * Unbinds the SupervisorInfo interface and removes a blade from both this Clif server and
130      * the Clif application it belongs to. Assumes that the blade is stopped.
131      * @param clifApp the Clif application distributed component the blade belongs to
132      * @param id blade identifier
133      */

134     public synchronized void removeBlade(Component clifApp, String JavaDoc id)
135         throws ClifException
136     {
137         Component blade = (Component) blades.remove(id);
138         if (blade == null)
139         {
140             throw new ClifException("Unknown blade identifier " + id);
141         }
142         try
143         {
144             Fractal.getBindingController(blade).unbindFc(SupervisorInfo.SUPERVISOR_INFO);
145             Fractal.getContentController(clifApp).removeFcSubComponent(blade);
146             if (blades.isEmpty())
147             {
148                 ClifClassLoader.clear();
149             }
150         }
151         catch (Exception JavaDoc ex)
152         {
153             blades.put(id, blade);
154             String JavaDoc message = "Unable to complete removal of blade " + id;
155             log.log(BasicLevel.ERROR, message, ex);
156             throw new ClifException(message, ex);
157         }
158     }
159 }
160
Popular Tags