KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > BasicFactory


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-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  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl;
25
26 import java.util.Map JavaDoc;
27
28 import org.objectweb.deployment.scheduling.core.api.Scheduler;
29 import org.objectweb.deployment.scheduling.component.api.FactoryProviderTask;
30 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask;
31 import org.objectweb.fractal.api.control.BindingController;
32
33 /**
34  * Basic implementation of the {@link Factory} interface. This implementation
35  * uses a {@link Loader} to load ADL definitions, a {@link Compiler} to compile
36  * them, and a {@link Scheduler} to execute the compiled tasks.
37  */

38
39 public class BasicFactory implements BindingController, Factory {
40
41   /**
42    * Name of the client interface bound to the {@link Loader} used by this
43    * factory.
44    */

45   
46   public final static String JavaDoc LOADER_BINDING = "loader";
47   
48   /**
49    * Name of the client interface bound to the {@link Compiler} used by this
50    * factory.
51    */

52   
53   public final static String JavaDoc COMPILER_BINDING = "compiler";
54   
55   /**
56    * Name of the client interface bound to the {@link Scheduler} used by this
57    * factory.
58    */

59   
60   public final static String JavaDoc SCHEDULER_BINDING = "scheduler";
61   
62   /**
63    * The {@link Loader} used by this factory.
64    */

65   
66   public Loader loader;
67   
68   /**
69    * The {@link Compiler} used by this factory.
70    */

71   
72   public Compiler JavaDoc compiler;
73   
74   /**
75    * The {@link Scheduler} used by this factory.
76    */

77   
78   public Scheduler scheduler;
79   
80   // --------------------------------------------------------------------------
81
// Implementation of the BindingController interface
82
// --------------------------------------------------------------------------
83

84   public String JavaDoc[] listFc() {
85     return new String JavaDoc[] { LOADER_BINDING, COMPILER_BINDING, SCHEDULER_BINDING };
86   }
87
88   public Object JavaDoc lookupFc (final String JavaDoc itf) {
89     if (itf.equals(LOADER_BINDING)) {
90       return loader;
91     } else if (itf.equals(COMPILER_BINDING)) {
92       return compiler;
93     } else if (itf.equals(SCHEDULER_BINDING)) {
94       return scheduler;
95     }
96     return null;
97   }
98
99   public void bindFc (final String JavaDoc itf, final Object JavaDoc value) {
100     if (itf.equals(LOADER_BINDING)) {
101       loader = (Loader)value;
102     } else if (itf.equals(COMPILER_BINDING)) {
103       compiler = (Compiler JavaDoc)value;
104     } else if (itf.equals(SCHEDULER_BINDING)) {
105       scheduler = (Scheduler)value;
106     }
107   }
108
109   public void unbindFc (final String JavaDoc itf) {
110     if (itf.equals(LOADER_BINDING)) {
111       loader = null;
112     } else if (itf.equals(COMPILER_BINDING)) {
113       compiler = null;
114     } else if (itf.equals(SCHEDULER_BINDING)) {
115       scheduler = null;
116     }
117   }
118   
119   // --------------------------------------------------------------------------
120
// Implementation of the Facfory interface
121
// --------------------------------------------------------------------------
122

123   public Object JavaDoc newComponentType (final String JavaDoc name, final Map JavaDoc context)
124     throws ADLException
125   {
126     Definition d = loader.load(name, context);
127     TaskMap m = new BasicTaskMap();
128     compiler.compile(d, m, context);
129     try {
130       m.getTask("type", d).execute(context);
131       return ((FactoryProviderTask)m.getTask("type", d)).getFactory();
132     } catch (Exception JavaDoc e) {
133       e.printStackTrace();
134     }
135     return null;
136   }
137
138   public Object JavaDoc newComponent (final String JavaDoc name, final Map JavaDoc context)
139     throws ADLException
140   {
141     Definition d = loader.load(name, context);
142     // DEBUG
143
/*
144     try {
145       java.io.PrintWriter pw = new java.io.PrintWriter(System.err, true);
146       new org.objectweb.fractal.adl.xml.XMLWriter(pw)
147         .write((org.objectweb.fractal.adl.Node)d);
148       pw.flush();
149     } catch (java.io.IOException e) {
150     }
151     */

152     TaskMap m = new BasicTaskMap();
153     compiler.compile(d, m, context);
154     /*
155     System.err.println();
156     org.objectweb.deployment.scheduling.core.api.Task[] ts = m.getTasks();
157     for (int i = 0; i < ts.length; ++i) {
158       System.err.println(ts[i]);
159     }
160     */

161     scheduler.schedule(m.getTasks(), context);
162     return ((InstanceProviderTask)m.getTask("create", d)).getInstance();
163   }
164 }
165
Popular Tags