KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > InOut > Impl > InOutImpl


1 /**
2  * InOutImpl.java is a part of the SOFA project.
3  * This file was created by pepan on 19.3.2003.
4  */

5 package SOFA.SOFAnode.InOut.Impl;
6
7 import java.util.HashMap JavaDoc;
8
9 import SOFA.Connector.ConnectorException;
10 import SOFA.Connector.Reference;
11 import SOFA.SOFAnode.InOut.Bundle;
12 import SOFA.SOFAnode.InOut.InOut2Client;
13 import SOFA.SOFAnode.InOut.InOut2InOut;
14 import SOFA.SOFAnode.InOut.InOutException;
15 import SOFA.SOFAnode.InOut.Connector.InOut2InOutConnector;
16 import SOFA.SOFAnode.TR.ComponentInfo;
17 import SOFA.SOFAnode.TR.TR2InOut;
18 import SOFA.SOFAnode.TR.Connector.TR2InOutConnector;
19 import SOFA.SOFAnode.TR.Impl.ComponentInfoImpl;
20
21 /**
22  * Provides services for storing bundles of components to TR and for retrieving them from TR.
23  * @author Petr Panuska
24  */

25 public class InOutImpl implements InOut2InOut, InOut2Client {
26
27   /**
28    * The interface between the InOut part of this SOFA node and the Template Repository part of the same node.
29    */

30   private TR2InOut tr;
31
32   /**
33    * A list of connections to other InOuts. Each key is a URL of a SOFA node,
34    * each value is the reference to the InOut interface of this SOFA node.
35    */

36   private HashMap JavaDoc inOutConn;
37
38   /**
39    * Creates the InOut server and connects it to TR.
40    * @param ref the connector to TR.
41    */

42   public InOutImpl (Reference ref) {
43     try {
44       tr = (TR2InOut) TR2InOutConnector.createClt(ref);
45     } catch (ConnectorException e) {
46       e.printStackTrace();
47       System.exit(-1);
48     }
49     inOutConn = new HashMap JavaDoc();
50   }
51
52   /**
53    * Pulls a bundle packing the specified components from this SOFA node.
54    * @param descs the list of components whose description is going to be pulled.
55    * @param comps the list of components which are going to be pulled (description including
56    * component files as well)
57    * @param inferiors
58    * @return a bundle containing required components.
59    * @throws InOutException if some error occured (specified components not present, etc.)
60    */

61   public Bundle pullBundle (ComponentInfo[] descs, ComponentInfo[] comps, boolean inferiors) throws InOutException {
62     return tr.getBundle(descs, comps, inferiors);
63   }
64
65   /**
66    * Pushes the specified bundle (packing components) to this SOFA node.
67    * @param bundle the bundle which is being pushed.
68    * @throws InOutException
69    */

70   public void pushBundle (Bundle bundle) throws InOutException {
71     tr.storeBundle(bundle);
72   }
73
74   /**
75    * Gets a connection to InOut server of a given SOFA node. If the connection
76    * does not exist, it is created first.
77    * @param sofaNode a string representation of a reference to an InOut2InOut connector of a SOFA node.
78    * @return an interface to the InOut part of the SOFA node.
79    * @see Reference#fromString(String)
80    */

81   private InOut2InOut getConnection (String JavaDoc sofaNode) {
82     InOut2InOut inOut;
83     inOut = (InOut2InOut) inOutConn.get(sofaNode); // get a previously created connection
84
if (inOut == null) { // the connection has not been created yet
85
try {
86         inOut = (InOut2InOut) InOut2InOutConnector.createClt(Reference.fromString(sofaNode)); // create it
87
} catch (ConnectorException e) {
88         throw (new InOutException("Could not connect to " + sofaNode + "\n", e));
89       }
90       inOutConn.put(sofaNode, inOut); // store the new created connection
91
}
92     return inOut;
93   }
94
95   /**
96    * Distributes a bundle to the given SOFA node.
97    * @param bundle the bundle which is going to be distributed.
98    * @param sofaNode a string representation of a reference to an InOut2InOut connector of a SOFA node.
99    * @throws InOutException when an error occurs.
100    */

101   private void distributeComponent (Bundle bundle, String JavaDoc sofaNode) throws InOutException {
102     InOut2InOut inOut = getConnection(sofaNode);
103     inOut.pushBundle(bundle);
104   }
105
106   /**
107    * Translates a given array of component full names (name[version]) into an array of
108    * {@link ComponentInfoImpl} objects. It creates new instances of these objects.
109    * @param fullNames the array of component full names.
110    * @return an array of component information objects.
111    */

112   private ComponentInfoImpl[] fullNamesToComponentInfos (String JavaDoc[] fullNames) {
113     ComponentInfoImpl[] comps = null;
114     if (fullNames != null) {
115       comps = new ComponentInfoImpl[fullNames.length];
116       for (int i = 0; i < comps.length; i++) {
117         comps[i] = ComponentInfoImpl.fromString(fullNames[i]);
118       }
119     }
120     return comps;
121   }
122
123   /**
124    * Obtains the specified components (or their description only) from the specified SOFA node.
125    * It may obtain more than just the explicitly asked components.
126    * @param offers full names (the name and the implementation version in the format "name[version]")
127    * of components whose description is to be obtained.
128    * @param components full names (the name and the implementation version in the format "name[version]")
129    * of components to be obtained (not only the description).
130    * @param sofaNode a string representation of a reference to an InOut2InOut connector of a SOFA node.
131    * It must not be <code>null</code>, otherwise an exception is thrown.
132    * @param inferiors if <code>true</code>, all sub-components are obtained as well; otherwise, the
133    * explicitly asked components are obtained only.
134    * @throws InOutException if an error when connecting to the SOFA node occurs.
135    */

136   private void _obtain (String JavaDoc[] offers, String JavaDoc[] components, String JavaDoc sofaNode, boolean inferiors) throws InOutException {
137     if (sofaNode == null) {
138       throw new InOutException("The SOFA node where components are to be distributed from must not be null!");
139     }
140
141     InOut2InOut inOut = getConnection(sofaNode);
142
143     ComponentInfoImpl[] descs = fullNamesToComponentInfos(offers);
144     ComponentInfoImpl[] comps = fullNamesToComponentInfos(components);
145
146     Bundle bundle = inOut.pullBundle(descs, comps, inferiors);
147     tr.storeBundle(bundle);
148   }
149
150   /**
151    * Distributes given components (or their description only) to given SOFA nodes.
152    * It may distribute the explicitly specified components only as well as all sub-components
153    * of the specified ones.
154    * @param offers full names (the name and the implementation version in the format "name[version]")
155    * of components whose description is to be distributed.
156    * @param components full names (the name and the implementation version in the format "name[version]")
157    * of components to be distributed (not only the description).
158    * @param sofaNodes string representations of references to InOut2InOut connectors of SOFA nodes.
159    * It must not be <code>null</code> nor an empty array, otherwise an exception is thrown.
160    * @param inferiors if <code>true</code>, all sub-components are obtained as well; otherwise, the
161    * explicitly asked components are obtained only.
162    * @throws InOutException if an error occurs or the array of SOFA nodes is empty or <code>null</code>.
163    */

164   private void _distribute (String JavaDoc[] offers, String JavaDoc[] components, String JavaDoc[] sofaNodes, boolean inferiors) throws InOutException {
165
166     if (sofaNodes == null || sofaNodes.length == 0) {
167       throw new InOutException("The list of SOFA nodes where components are to be distributed to must not be null nor empty!");
168     }
169
170     ComponentInfoImpl[] descs = fullNamesToComponentInfos(offers);
171     ComponentInfoImpl[] comps = fullNamesToComponentInfos(components);
172
173     Bundle bundle = tr.getBundle(descs, comps, inferiors);
174     for (int i = 0; i < sofaNodes.length; i++) {
175       String JavaDoc sofaNode = sofaNodes[i];
176       distributeComponent(bundle, sofaNode);
177     }
178   }
179
180   /**
181    * Obtains the specified components (or their description only) from the specified SOFA node.
182    * It obtains the explicitly asked components only.
183    * @param offers full names (the name and the implementation version in the format "name[version]")
184    * of components whose description is to be obtained.
185    * @param components full names (the name and the implementation version in the format "name[version]")
186    * of components to be obtained (not only the description).
187    * @param sofaNode a string representation of a reference to an InOut2InOut connector of a SOFA node.
188    * It must not be <code>null</code>, otherwise an exception is thrown.
189    * @throws InOutException if an error when connecting to the SOFA node occurs.
190    */

191   public void obtain (String JavaDoc[] offers, String JavaDoc[] components, String JavaDoc sofaNode) throws InOutException {
192     _obtain(offers, components, sofaNode, false);
193   }
194
195   /**
196    * Distributes given components (or their description only) to given SOFA nodes.
197    * It distributes the explicitly specified components only.
198    * @param offers full names (the name and the implementation version in the format "name[version]")
199    * of components whose description is to be distributed.
200    * @param components full names (the name and the implementation version in the format "name[version]")
201    * of components to be distributed (not only the description).
202    * @param sofaNodes string representations of references to InOut2InOut connectors of SOFA nodes.
203    * It must not be <code>null</code> nor an empty array, otherwise an exception is thrown.
204    * @throws InOutException if an error occurs or the array of SOFA nodes is empty or <code>null</code>.
205    */

206   public void distribute (String JavaDoc[] offers, String JavaDoc[] components, String JavaDoc[] sofaNodes) throws InOutException {
207     _distribute(offers, components, sofaNodes, false);
208   }
209
210   /**
211    * Obtains descriptions of the specified components and all their sub-components from the specified SOFA node
212    * @param offers full names (the name and the implementation version in the format "name[version]")
213    * of components whose description is to be obtained.
214    * @param sofaNode a string representation of a reference to an InOut2InOut connector of a SOFA node.
215    * It must not be <code>null</code>, otherwise an exception is thrown.
216    * @throws InOutException if an error when connecting to the SOFA node occurs.
217    */

218   public void obtainOffers (String JavaDoc[] offers, String JavaDoc sofaNode) throws InOutException {
219     _obtain(offers, null, sofaNode, true);
220   }
221
222   /**
223    * Distributes descriptions of the given components and all their sub-components to the given SOFA nodes.
224    * @param offers full names (the name and the implementation version in the format "name[version]")
225    * of components whose description is to be distributed.
226    * @param sofaNodes string representations of references to InOut2InOut connectors of SOFA nodes.
227    * It must not be <code>null</code> nor an empty array, otherwise an exception is thrown.
228    * @throws InOutException if an error occurs or the array of SOFA nodes is empty or <code>null</code>.
229    */

230   public void distributeOffers (String JavaDoc[] offers, String JavaDoc[] sofaNodes) throws InOutException {
231     _distribute(offers, null, sofaNodes, true);
232   }
233
234   /**
235    * Obtains the specified components and all their sub-components from the specified SOFA node.
236    * @param components full names (the name and the implementation version in the format "name[version]")
237    * of components to be obtained (not only the description).
238    * @param sofaNode a string representation of a reference to an InOut2InOut connector of a SOFA node.
239    * It must not be <code>null</code>, otherwise an exception is thrown.
240    * @throws InOutException if an error when connecting to the SOFA node occurs.
241    */

242   public void obtainComponents (String JavaDoc[] components, String JavaDoc sofaNode) throws InOutException {
243     _obtain(null, components, sofaNode, true);
244   }
245
246   /**
247    * Distributes given components and all their sub-components to the given SOFA nodes.
248    * @param components full names (the name and the implementation version in the format "name[version]")
249    * of components to be distributed (not only the description).
250    * @param sofaNodes string representations of references to InOut2InOut connectors of SOFA nodes.
251    * It must not be <code>null</code> nor an empty array, otherwise an exception is thrown.
252    * @throws InOutException if an error occurs or the array of SOFA nodes is empty or <code>null</code>.
253    */

254   public void distributeComponents (String JavaDoc[] components, String JavaDoc[] sofaNodes) throws InOutException {
255     _distribute(null, components, sofaNodes, true);
256   }
257
258   /**
259    * Gets a bundle containing a desription of all components installed on this SOFA node.
260    * @return a bundle decribing all components.
261    */

262   public Bundle list () {
263     return tr.list();
264   }
265
266 }
267
Popular Tags