KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > smi > Misc


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.smi;
29
30
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.CORBA.Any JavaDoc;
33 import org.omg.CfMAF.*;
34 import org.objectweb.mobilitools.smi.idl.*;
35 import org.objectweb.mobilitools.smi.api.Constants;
36 import java.util.Properties JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.MalformedURLException JavaDoc;
41
42
43 abstract public class Misc
44 {
45     static public final Name DUMMYNAME = newDummyName();
46     static public final AgentProfile DUMMYAGENTPROFILE = newDummyAgentProfile();
47     static public final AgentSystemInfo DUMMYAGENTSYSTEMINFO = newDummyAgentSystemInfo();
48
49
50     static public Name newDummyName()
51     {
52         return new Name(new byte[0], new byte[0], (short)0);
53     }
54
55
56     static public AgentProfile newDummyAgentProfile()
57     {
58         return new AgentProfile(
59             (short)0,
60             (short)0,
61             "",
62             (short)0,
63             (short)0,
64             (short)0,
65             new Any JavaDoc[0]);
66     }
67
68
69     static public AgentSystemInfo newDummyAgentSystemInfo()
70     {
71         return new AgentSystemInfo(
72             newDummyName(),
73             (short)0,
74             new LanguageMap[0],
75             "",
76             (short)0,
77             (short)0,
78             new Any JavaDoc[0]);
79     }
80
81
82     static public Properties JavaDoc any2property(Any JavaDoc[] any)
83     {
84         Properties JavaDoc result = new Properties JavaDoc();
85         if (any != null)
86         {
87             for (int i=0 ; i<any.length ; ++i)
88             {
89                 SMIProperty prop = SMIPropertyHelper.extract(any[i]);
90                 result.setProperty(prop.name, prop.value);
91             }
92         }
93         return result;
94     }
95
96
97     static public Any JavaDoc[] property2any(Properties JavaDoc properties, ORB JavaDoc orb)
98     {
99         Any JavaDoc[] result;
100         if (properties != null)
101         {
102             result = new Any JavaDoc[properties.size()];
103             Enumeration JavaDoc names = properties.propertyNames();
104             for (int i=0 ; names.hasMoreElements() ; ++i)
105             {
106                 String JavaDoc name = (String JavaDoc)names.nextElement();
107                 String JavaDoc value = properties.getProperty(name);
108                 if (value == null)
109                 {
110                     value = "";
111                 }
112                 result[i] = orb.create_any();
113                 SMIPropertyHelper.insert(result[i], new SMIProperty(name, value));
114             }
115         }
116         else
117         {
118             result = new Any JavaDoc[0];
119         }
120         return result;
121     }
122
123
124     /**
125         @return true if profile (i.e. AgentProfile or AgentSystemInfo) value
126         matches mask profile, false otherwise.
127         @param mask a AgentSystemInfo or AgentProfile instance, which may contain
128         zero values (i.e. zero for integer fields, empty string for string fields).
129         A null mask matches any profile, provided that the Profile type is the same
130         (i.e. AgentProfile for both the mask and the value, or AgentSystemInfo for both).
131         @param value an actual, complete AgentSystemInfo or AgentProfile instance,
132         that has to be matched against the mask parameter. A null value matches with no mask
133         but the null mask.
134     */

135     static public boolean matchProfile(Object JavaDoc mask, Object JavaDoc value)
136     {
137         boolean result = false;
138         if (mask == null)
139         {
140             result = true;
141         }
142         else if (value != null)
143         {
144             if (mask instanceof AgentProfile && value instanceof AgentProfile)
145             {
146                 AgentProfile m = (AgentProfile)mask;
147                 AgentProfile v = (AgentProfile)value;
148                 result =
149                     (m.language_id == 0 || m.language_id == v.language_id) &&
150                     (m.agent_system_type == 0 || m.agent_system_type == v.agent_system_type) &&
151                     (m.major_version == 0 || m.major_version == v.major_version) &&
152                     (m.minor_version == 0 || m.minor_version == v.minor_version) &&
153                     (m.serialization == 0 || m.serialization == v.serialization) &&
154                     matchProperties(Misc.any2property(m.properties), Misc.any2property(v.properties));
155             }
156             else if (mask instanceof AgentSystemInfo && value instanceof AgentSystemInfo)
157             {
158                 AgentSystemInfo m = (AgentSystemInfo)mask;
159                 AgentSystemInfo v = (AgentSystemInfo)value;
160
161                 result =
162                     (m.agent_system_type == 0 || m.agent_system_type == v.agent_system_type) &&
163                     (m.major_version == 0 || m.major_version == v.major_version) &&
164                     (m.minor_version == 0 || m.minor_version == v.minor_version) &&
165                     matchProperties(Misc.any2property(m.properties), Misc.any2property(v.properties));
166             }
167         }
168         return result;
169     }
170
171
172     static public boolean matchProperties(Properties JavaDoc subset, Properties JavaDoc set)
173     {
174         Enumeration JavaDoc keys = subset.propertyNames();
175         while (keys.hasMoreElements())
176         {
177             String JavaDoc key = (String JavaDoc)keys.nextElement();
178             if (!subset.getProperty(key).equals(set.getProperty(key)))
179             {
180                 return false;
181             }
182         }
183         return true;
184     }
185
186
187     /**
188      * Invokes the ClassLoader factory (as set by the appropriate system
189      * property) to get a classloader for the given parameters. Note that
190      * parameters may have various interpretations, or may be ignored, depending
191      * on the actual class loader.
192      * @param parent the returned classloader's parent
193      * @param codebase the codebase (form depends on the actual classloader)
194      * @param profile the agent profile (see MAF spec.)
195      * @param provider the MAFAgentSystem implementation providing where to get
196      * classes from
197      * @return the appropriate classloader instance
198      * @see org.objectweb.mobilitools.smi.lib.SMIClassLoader
199      * @see org.objectweb.mobilitools.smi.lib.RMIClassLoaderAdapter
200      * @see org.objectweb.mobilitools.smi.lib.URLClassLoaderAdapter
201      * @see org.objectweb.mobilitools.smi.api.Constants
202      */

203     static public ClassLoader JavaDoc getClassLoader(
204         ClassLoader JavaDoc parent,
205         String JavaDoc codebase,
206         AgentProfile profile,
207         MAFAgentSystem provider)
208     {
209         ClassLoader JavaDoc loader = null;
210         String JavaDoc loader_name = System.getProperty(
211             Constants.classLoaderProp,
212             Constants.classLoaderDefault);
213         try
214         {
215             loader = (ClassLoader JavaDoc)Class.forName(loader_name)
216                 .getMethod(
217                     Constants.classLoaderMethod,
218                     new Class JavaDoc[] {
219                         ClassLoader JavaDoc.class,
220                         String JavaDoc.class,
221                         AgentProfile.class,
222                         MAFAgentSystem.class })
223                 .invoke(
224                     null,
225                     new Object JavaDoc[] {
226                         parent,
227                         codebase,
228                         profile,
229                         provider});
230         }
231         catch (Throwable JavaDoc ex)
232         {
233             throw new Error JavaDoc(
234                 "Bad classloader factory or bad parameters: "
235                 + loader_name
236                 + "." + Constants.classLoaderMethod
237                 + "("
238                 + parent + ", "
239                 + codebase + ", "
240                 + profile + ", "
241                 + provider
242                 + ")\n" + ex);
243         }
244         return loader;
245     }
246
247
248     static public URL JavaDoc[] codebase2URLs(String JavaDoc codebase)
249         throws MalformedURLException JavaDoc
250     {
251         StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(codebase, " ");
252         URL JavaDoc[] urls = new URL JavaDoc[parser.countTokens()];
253         int i=0;
254         while (parser.hasMoreTokens())
255         {
256             urls[i++] = new URL JavaDoc(parser.nextToken());
257         }
258         return urls;
259     }
260 }
261
Popular Tags