KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > description > Plug


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

18 package org.objectweb.kilim.description;
19
20 import java.util.LinkedHashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.objectweb.kilim.KilimException;
24
25 /**
26  * @author horn
27  * Describes a slot, which is just a set of unbound ports in a new naming context
28  */

29 public class Plug extends TemplateElementImpl {
30     private static String JavaDoc[][] msgTexts = {
31         { "attempt to create a plug with a null slot name", "" }, //0
32
{ "attempt to plug a null instance name ", "" }, //1
33
{ "attempt to define a plug to a slot ", " in a null template" }, //2
34
{ "attempt to add a mapping with a null external name in plug of slot ", "" }, //3
35
{ "attempt to add an already added name mapping : ", " in slot " }, //4
36
{ "attempt to remove a null nam mapping from a plug to slot ", "" }, //5
37
{ "attempt to remove a name mapping from a plug to ", " without name mapping in template " } //6
38
};
39     
40     private String JavaDoc slotName;
41     private String JavaDoc instanceName;
42     private LinkedHashMap JavaDoc mappings;
43     
44     /**
45      * Method Plug.
46      * @param aSlotName : the name of the slot
47      * @param aInstanceName : the name of the component to be plugged.
48      * @param aTemplate : the template in which the plug is done.
49      * @throws KilimException : generates an exception if an argument is null.
50      */

51     public Plug(String JavaDoc aSlotName, String JavaDoc aInstanceName, TemplateDescription aTemplate) throws KilimException {
52         if (aSlotName == null) {
53             throw new KilimException(msgTexts[0][0]);
54         }
55         if (aInstanceName == null) {
56             throw new KilimException(msgTexts[1][0] + msgSuffix1());
57         }
58         if (aTemplate == null) {
59             throw new KilimException(msgTexts[2][0] + aSlotName + msgTexts[2][1]);
60         }
61             
62         slotName = aSlotName;
63         instanceName = aInstanceName;
64         setContainingTemplate(aTemplate);
65     }
66         
67     /**
68      * returns the slot name.
69      * @return String
70      */

71     public String JavaDoc getSlotName() {
72         return slotName;
73     }
74     
75     /**
76      * returns the component name.
77      * @return String
78      */

79     public String JavaDoc getInstanceName() {
80         return instanceName;
81     }
82         
83     /**
84      * A name mapping is a correspondance between the name of an interface defined in a slot and the corresponding port in a component.
85      * This correspondance has been introduced to relax constraints on the interface naming during plug operations. This method returns the
86      * name of component interface that will be associate to interface aName in the slot.
87      * @param aName : the local name of the interface declared in the slot.
88      * @return String the local name of the corresponding interface in the component.
89      */

90     public String JavaDoc getNameMapping(String JavaDoc aName) {
91         if (mappings == null) {
92             return aName;
93         }
94         
95         Object JavaDoc name1 = mappings.get(aName);
96         if (name1 == null) {
97             return aName;
98         } else {
99             return (String JavaDoc) name1;
100         }
101     }
102     
103     /**
104      * A name mapping is a correspondance between the name of an interface defined in a slot and the corresponding port in a component.
105      * This correspondance has been introduced to relax constraints on the interface naming during plug operations. This method returns as
106      * an iterator the name mappings between slot ports and the plugged component ports.
107      * @return Iterator
108      */

109     public Iterator JavaDoc getNameMappings() {
110         if (mappings == null) {
111             return KILIM.EMPTY_ITERATOR;
112         }
113         return mappings.values().iterator();
114     }
115
116     /**
117      * A name mapping is a correspondance between the name of an interface defined in a slot and the corresponding port in a component.
118      * This correspondance has been introduced to relax constraints on the interface naming during plug operations. This method adds a new name
119      * mapping.
120      * @param aExternal :name of the slot port
121      * @param aInternal : name of the component port.
122      * @throws KilimException : generated when aExternal or aInternal is null
123      */

124     public void addNameMapping(String JavaDoc aExternal, String JavaDoc aInternal) throws KilimException {
125         if (aExternal == null) {
126             throw new KilimException(msgTexts[3][0] + msgSuffix1());
127         }
128         if (mappings == null) {
129             mappings = new LinkedHashMap JavaDoc();
130         }
131         
132         if (mappings.containsKey(aExternal)) {
133             throw new KilimException(msgTexts[4][0] + aExternal + " to " + aInternal + msgTexts[4][1] + msgSuffix1());
134         }
135         mappings.put(aExternal, aInternal);
136     }
137     
138     /**
139      * A name mapping is a correspondance between the name of an interface defined in a slot and the corresponding port in a component.
140      * This correspondance has been introduced to relax constraints on the interface naming during plug operations. This method removes a name
141      * mapping.
142      * @param aExternal : name of the mapping to be removed (name mapping are identified by the slot name of a port).
143      * @throws KilimException : generated if aExternal is null.
144      */

145     public void removeNameMapping(String JavaDoc aExternal) throws KilimException {
146         if (aExternal == null) {
147             throw new KilimException(msgTexts[5][0] + msgSuffix1());
148         }
149         
150         if (mappings == null) {
151             throw new KilimException(msgTexts[6][0] + slotName + msgTexts[6][1] + msgSuffix1());
152         }
153         mappings.remove(aExternal);
154     }
155     
156     private String JavaDoc msgSuffix1() {
157         return slotName + " in template " + getContainingTemplate();
158     }
159 }
Popular Tags