KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > descriptor > data > ProActiveDescriptorImpl


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.descriptor.data;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.core.ProActiveException;
36 import org.objectweb.proactive.core.process.ExternalProcess;
37 import org.objectweb.proactive.core.process.ExternalProcessDecorator;
38 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
39 import org.objectweb.proactive.ext.security.PolicyServer;
40 import org.objectweb.proactive.ext.security.ProActiveSecurityDescriptorHandler;
41
42 import org.xml.sax.SAXException JavaDoc;
43
44 import java.io.IOException JavaDoc;
45
46 import java.security.cert.X509Certificate JavaDoc;
47
48 import java.util.Collection JavaDoc;
49 import java.util.Iterator JavaDoc;
50
51
52 /**
53  * <p>
54  * A <code>ProactiveDescriptor</code> is an internal representation of XML
55  * Descriptor. It offers a set of services to access/activate/desactivate
56  * <code>VirtualNode</code>.
57  * </p>
58  *
59  * @author ProActive Team
60  * @version 1.0, 2002/09/20
61  * @since ProActive 0.9.3
62  * @see VirtualNode
63  * @see VirtualMachine
64  */

65 public class ProActiveDescriptorImpl implements ProActiveDescriptor {
66     //
67
// ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
68
//
69
protected static Logger logger = Logger.getLogger(ProActiveDescriptorImpl.class.getName());
70
71     /** map virtualNode name and objects */
72     private java.util.HashMap JavaDoc virtualNodeMapping;
73
74     /** map jvm name and object */
75     private java.util.HashMap JavaDoc virtualMachineMapping;
76
77     /** map process id and process */
78     private java.util.HashMap JavaDoc processMapping;
79
80     /** map process id and process updater for later update of the process */
81     private java.util.HashMap JavaDoc pendingProcessMapping;
82
83     /** Location of the xml file */
84     private String JavaDoc url;
85
86     /** security rules */
87     public PolicyServer policyServer;
88     public X509Certificate JavaDoc creatorCertificate;
89     public String JavaDoc securityFile;
90
91     //
92
// ----- CONSTRUCTORS -----------------------------------------------------------------------------------
93
//
94

95     /**
96      * Contructs a new intance of ProActiveDescriptor
97      */

98     public ProActiveDescriptorImpl(String JavaDoc url) {
99         virtualNodeMapping = new java.util.HashMap JavaDoc();
100         virtualMachineMapping = new java.util.HashMap JavaDoc();
101         processMapping = new java.util.HashMap JavaDoc();
102         pendingProcessMapping = new java.util.HashMap JavaDoc();
103         this.url = url;
104     }
105
106     //
107
// ----- PUBLIC METHODS -----------------------------------------------------------------------------------
108
//
109
public VirtualNode[] getVirtualNodes() {
110         int i = 0;
111         VirtualNode[] virtualNodeArray = new VirtualNode[virtualNodeMapping.size()];
112         Collection JavaDoc collection = virtualNodeMapping.values();
113         for (Iterator JavaDoc iter = collection.iterator(); iter.hasNext();) {
114             virtualNodeArray[i] = (VirtualNode) iter.next();
115             i++;
116         }
117         return virtualNodeArray;
118     }
119
120     public VirtualNode getVirtualNode(String JavaDoc name) {
121         return (VirtualNode) virtualNodeMapping.get(name);
122     }
123
124     public VirtualMachine getVirtualMachine(String JavaDoc name) {
125         return (VirtualMachine) virtualMachineMapping.get(name);
126     }
127
128     public ExternalProcess getProcess(String JavaDoc name) {
129         return (ExternalProcess) processMapping.get(name);
130     }
131
132     public VirtualNode createVirtualNode(String JavaDoc vnName, boolean lookup) {
133         VirtualNode vn = getVirtualNode(vnName);
134         if (vn == null) {
135             if (lookup) {
136                 vn = new VirtualNodeLookup(vnName);
137             } else {
138                 vn = new VirtualNodeImpl(vnName, creatorCertificate,
139                         policyServer);
140             }
141
142             virtualNodeMapping.put(vnName, vn);
143             if (logger.isInfoEnabled()) {
144                 logger.info("created VirtualNode name=" + vnName);
145             }
146         }
147         return vn;
148     }
149
150     public VirtualMachine createVirtualMachine(String JavaDoc vmName) {
151         VirtualMachine vm = getVirtualMachine(vmName);
152         if (vm == null) {
153             vm = new VirtualMachineImpl();
154             vm.setName(vmName);
155             virtualMachineMapping.put(vmName, vm);
156         }
157         if (logger.isDebugEnabled()) {
158             logger.debug("created virtualMachine name=" + vmName);
159         }
160         return vm;
161     }
162
163     public ExternalProcess createProcess(String JavaDoc processID,
164         String JavaDoc processClassName) throws ProActiveException {
165         ExternalProcess process = getProcess(processID);
166         if (process == null) {
167             process = createProcess(processClassName);
168             addExternalProcess(processID, process);
169         }
170         return process;
171     }
172
173     public ExternalProcess createProcess(String JavaDoc processClassName)
174         throws ProActiveException {
175         try {
176             Class JavaDoc processClass = Class.forName(processClassName);
177             return (ExternalProcess) processClass.newInstance();
178         } catch (ClassNotFoundException JavaDoc e) {
179             throw new ProActiveException(e);
180         } catch (InstantiationException JavaDoc e) {
181             throw new ProActiveException(e);
182         } catch (IllegalAccessException JavaDoc e) {
183             throw new ProActiveException(e);
184         }
185     }
186
187     public void registerProcess(VirtualMachine virtualMachine, String JavaDoc processID) {
188         ExternalProcess process = getProcess(processID);
189         if (process == null) {
190             addPendingProcess(processID, virtualMachine);
191         } else {
192             virtualMachine.setProcess(process);
193         }
194     }
195
196     public void registerProcess(ExternalProcessDecorator compositeProcess,
197         String JavaDoc processID) {
198         ExternalProcess process = getProcess(processID);
199         if (process == null) {
200             addPendingProcess(processID, compositeProcess);
201         } else {
202             compositeProcess.setTargetProcess(process);
203         }
204     }
205
206     public void activateMappings() {
207         VirtualNode[] virtualNodeArray = getVirtualNodes();
208         for (int i = 0; i < virtualNodeArray.length; i++) {
209             virtualNodeArray[i].activate();
210         }
211     }
212
213     public void activateMapping(String JavaDoc virtualNodeName) {
214         VirtualNode virtualNode = getVirtualNode(virtualNodeName);
215         virtualNode.activate();
216     }
217
218     public void killall(boolean softly) throws ProActiveException {
219         ProActiveRuntimeImpl part = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime();
220         part.removeDescriptor(this.url);
221         VirtualNode[] vnArray = getVirtualNodes();
222         for (int i = 0; i < vnArray.length; i++) {
223             vnArray[i].killAll(softly);
224             virtualNodeMapping.remove(vnArray[i].getName());
225             //vnArray[i] = null;
226
}
227     }
228
229     /**
230      * Returns the size of virualNodeMapping HashMap
231      * @return int
232      */

233     public int getVirtualNodeMappingSize() {
234         return virtualNodeMapping.size();
235     }
236
237     // SECURITY
238

239     /**
240      * Intialize application security policy
241      * @param file
242      */

243     public void createPolicyServer(String JavaDoc file) {
244         securityFile = file;
245         try {
246             policyServer = ProActiveSecurityDescriptorHandler.createPolicyServer(file);
247             // policyServer = ps;
248
// ProActiveRuntime prR = RuntimeFactory.getDefaultRuntime();
249
// ProActiveSecurityManager psm = new ProActiveSecurityManager(file);
250
// prR.setProActiveSecurityManager(psm);
251
// } catch (ProActiveException e) {
252
// e.printStackTrace();
253
} catch (IOException JavaDoc e) {
254             // TODO Auto-generated catch block
255
e.printStackTrace();
256         } catch (SAXException JavaDoc e) {
257             // TODO Auto-generated catch block
258
e.printStackTrace();
259         }
260     }
261
262     public PolicyServer getPolicyServer() {
263         return policyServer;
264     }
265
266     /* (non-Javadoc)
267      * @see org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor#getSecurityFilePath()
268      */

269     public String JavaDoc getSecurityFilePath() {
270         return securityFile;
271     }
272
273     //
274
// ----- PROTECTED METHODS -----------------------------------------------------------------------------------
275
//
276
//
277
// ----- PRIVATE METHODS -----------------------------------------------------------------------------------
278
//
279
private void addExternalProcess(String JavaDoc processID, ExternalProcess process) {
280         ProcessUpdater processUpdater = (ProcessUpdater) pendingProcessMapping.remove(processID);
281         if (processUpdater != null) {
282             if (logger.isDebugEnabled()) {
283                 logger.debug("Updating Process name=" + processID);
284             }
285             processUpdater.updateProcess(process);
286         }
287         processMapping.put(processID, process);
288     }
289
290     private void addPendingProcess(String JavaDoc processID,
291         VirtualMachine virtualMachine) {
292         ProcessUpdater updater = new VirtualMachineProcessUpdater(virtualMachine);
293
294         //pendingProcessMapping.put(processID, updater);
295
addUpdater(processID, updater);
296     }
297
298     private void addPendingProcess(String JavaDoc processID,
299         ExternalProcessDecorator compositeProcess) {
300         ProcessUpdater updater = new CompositeExternalProcessUpdater(compositeProcess);
301
302         //pendingProcessMapping.put(processID, updater);
303
addUpdater(processID, updater);
304     }
305
306     private void addUpdater(String JavaDoc processID, ProcessUpdater processUpdater) {
307         CompositeProcessUpdater compositeProcessUpdater = (CompositeProcessUpdater) pendingProcessMapping.get(processID);
308         if (compositeProcessUpdater == null) {
309             compositeProcessUpdater = new CompositeProcessUpdater();
310             //pendingProcessMapping.put(processID, processUpdater);
311
pendingProcessMapping.put(processID, compositeProcessUpdater);
312         }
313         compositeProcessUpdater.addProcessUpdater(processUpdater);
314     }
315
316     //
317
// ----- INNER CLASSES -----------------------------------------------------------------------------------
318
//
319
private interface ProcessUpdater {
320         public void updateProcess(ExternalProcess p);
321     }
322
323     private class CompositeProcessUpdater implements ProcessUpdater {
324         private java.util.ArrayList JavaDoc updaterList;
325
326         public CompositeProcessUpdater() {
327             updaterList = new java.util.ArrayList JavaDoc();
328         }
329
330         public void addProcessUpdater(ProcessUpdater p) {
331             updaterList.add(p);
332         }
333
334         public void updateProcess(ExternalProcess p) {
335             java.util.Iterator JavaDoc it = updaterList.iterator();
336             while (it.hasNext()) {
337                 ProcessUpdater processUpdater = (ProcessUpdater) it.next();
338                 processUpdater.updateProcess(p);
339             }
340             updaterList.clear();
341         }
342     }
343
344     private class CompositeExternalProcessUpdater implements ProcessUpdater {
345         private ExternalProcessDecorator compositeExternalProcess;
346
347         public CompositeExternalProcessUpdater(
348             ExternalProcessDecorator compositeExternalProcess) {
349             this.compositeExternalProcess = compositeExternalProcess;
350         }
351
352         public void updateProcess(ExternalProcess p) {
353             if (logger.isDebugEnabled()) {
354                 logger.debug("Updating CompositeExternal Process");
355             }
356             compositeExternalProcess.setTargetProcess(p);
357         }
358     }
359
360     private class VirtualMachineProcessUpdater implements ProcessUpdater {
361         private VirtualMachine virtualMachine;
362
363         public VirtualMachineProcessUpdater(VirtualMachine virtualMachine) {
364             this.virtualMachine = virtualMachine;
365         }
366
367         public void updateProcess(ExternalProcess p) {
368             if (logger.isDebugEnabled()) {
369                 logger.debug("Updating VirtualMachine Process");
370             }
371             virtualMachine.setProcess(p);
372         }
373     }
374 }
375
Popular Tags