KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > repository > lib > BasicRepository


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 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: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.repository.lib;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.fractal.api.control.BindingController;
31 import org.objectweb.fractal.gui.graph.model.GraphModel;
32 import org.objectweb.fractal.gui.model.Component;
33 import org.objectweb.fractal.gui.model.Factory;
34 import org.objectweb.fractal.gui.model.Interface;
35 import org.objectweb.fractal.gui.repository.api.Repository;
36 import org.objectweb.fractal.gui.repository.api.Storage;
37
38 /**
39  * Basic implementation of the {@link Repository} interface. This implementation
40  * supposes that the component definitions are stored in the repository as
41  * Fractal ADL definitions, in XML form.
42  */

43
44 public class BasicRepository implements Repository, BindingController {
45
46   /**
47    * A mandatory client interface bound to a {@link Storage storage}. This is
48    * the storage from which this repository loads the component definitions.
49    */

50
51   public final static String JavaDoc STORAGE_BINDING = "storage";
52
53   /**
54    * TODO javadoc.
55    */

56   
57   public final static String JavaDoc DEFINITION_FACTORY_BINDING = "definition-factory";
58   
59   /**
60    * A mandatory client interface bound to a {@link Factory factory}. This
61    * factory is used to create components from the component definitions stored
62    * in the storage.
63    */

64
65   public final static String JavaDoc CONFIGURATION_FACTORY_BINDING = "configuration-factory";
66
67   /**
68    * The storage client interface.
69    */

70
71   private Storage storage;
72
73   /**
74    * TODO javadoc.
75    */

76   
77   private org.objectweb.fractal.adl.Factory definitionFactory;
78
79   /**
80    * The factory client interface.
81    */

82
83   private Factory configurationFactory;
84
85   // -------------------------------------------------------------------------
86
// Implementation of the BindingController interface
87
// -------------------------------------------------------------------------
88

89   public String JavaDoc[] listFc () {
90     return new String JavaDoc[] {
91       STORAGE_BINDING,
92       DEFINITION_FACTORY_BINDING,
93       CONFIGURATION_FACTORY_BINDING
94     };
95   }
96
97   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
98     if (STORAGE_BINDING.equals(clientItfName)) {
99       return storage;
100     } else if (DEFINITION_FACTORY_BINDING.equals(clientItfName)) {
101       return definitionFactory;
102     } else if (CONFIGURATION_FACTORY_BINDING.equals(clientItfName)) {
103       return configurationFactory;
104     }
105     return null;
106   }
107
108   public void bindFc (
109     final String JavaDoc clientItfName,
110     final Object JavaDoc serverItf)
111   {
112     if (STORAGE_BINDING.equals(clientItfName)) {
113       storage = (Storage)serverItf;
114     } else if (DEFINITION_FACTORY_BINDING.equals(clientItfName)) {
115       definitionFactory = (org.objectweb.fractal.adl.Factory)serverItf;
116     } else if (CONFIGURATION_FACTORY_BINDING.equals(clientItfName)) {
117       configurationFactory = (Factory)serverItf;
118     }
119   }
120
121   public void unbindFc (final String JavaDoc clientItfName) {
122     if (STORAGE_BINDING.equals(clientItfName)) {
123       storage = null;
124     } else if (DEFINITION_FACTORY_BINDING.equals(clientItfName)) {
125       definitionFactory = null;
126     } else if (CONFIGURATION_FACTORY_BINDING.equals(clientItfName)) {
127       configurationFactory = null;
128     }
129   }
130
131   // -------------------------------------------------------------------------
132
// Implementation of the Repository interface
133
// -------------------------------------------------------------------------
134

135   public Component loadComponent (
136     final String JavaDoc name,
137     final GraphModel graph) throws Exception JavaDoc
138   {
139     Map JavaDoc context = new HashMap JavaDoc();
140     context.put("factory", configurationFactory);
141     if (graph != null) {
142       context.put("graph", graph);
143     }
144     Component c = (Component)definitionFactory.newComponent(name, context);
145     removeGeneratedNames(c);
146     return c;
147   }
148
149   public String JavaDoc storeComponent (
150     final Component component,
151     final GraphModel graph,
152     final Object JavaDoc hints) throws Exception JavaDoc
153   {
154     FractalAdlWriter writer = new FractalAdlWriter();
155     writer.storage = storage;
156     writer.forceInternal = "inline".equals(hints);
157     return writer.saveTemplate(component, graph);
158   }
159   
160   private void removeGeneratedNames (Component c) {
161     if (c.getName().startsWith("GENERATED-")) {
162       c.setName("");
163     }
164     List JavaDoc l = c.getClientInterfaces();
165     for (int i = 0; i < l.size(); ++i) {
166       Interface itf = (Interface)l.get(i);
167       if (itf.getName().startsWith("GENERATED-")) {
168         itf.setName("");
169       }
170     }
171     l = c.getServerInterfaces();
172     for (int i = 0; i < l.size(); ++i) {
173       Interface itf = (Interface)l.get(i);
174       if (itf.getName().startsWith("GENERATED-")) {
175         itf.setName("");
176       }
177     }
178     l = c.getSubComponents();
179     for (int i = 0; i < l.size(); ++i) {
180       removeGeneratedNames((Component)l.get(i));
181     }
182   }
183 }
184
Popular Tags