1 26 27 28 package org.objectweb.mobilitools.smi; 29 30 31 import org.omg.CORBA.ORB ; 32 import org.omg.CORBA.Any ; 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 ; 37 import java.util.Enumeration ; 38 import java.util.StringTokenizer ; 39 import java.net.URL ; 40 import java.net.MalformedURLException ; 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 [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 [0]); 79 } 80 81 82 static public Properties any2property(Any [] any) 83 { 84 Properties result = new Properties (); 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 [] property2any(Properties properties, ORB orb) 98 { 99 Any [] result; 100 if (properties != null) 101 { 102 result = new Any [properties.size()]; 103 Enumeration names = properties.propertyNames(); 104 for (int i=0 ; names.hasMoreElements() ; ++i) 105 { 106 String name = (String )names.nextElement(); 107 String 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 [0]; 119 } 120 return result; 121 } 122 123 124 135 static public boolean matchProfile(Object mask, Object 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 subset, Properties set) 173 { 174 Enumeration keys = subset.propertyNames(); 175 while (keys.hasMoreElements()) 176 { 177 String key = (String )keys.nextElement(); 178 if (!subset.getProperty(key).equals(set.getProperty(key))) 179 { 180 return false; 181 } 182 } 183 return true; 184 } 185 186 187 203 static public ClassLoader getClassLoader( 204 ClassLoader parent, 205 String codebase, 206 AgentProfile profile, 207 MAFAgentSystem provider) 208 { 209 ClassLoader loader = null; 210 String loader_name = System.getProperty( 211 Constants.classLoaderProp, 212 Constants.classLoaderDefault); 213 try 214 { 215 loader = (ClassLoader )Class.forName(loader_name) 216 .getMethod( 217 Constants.classLoaderMethod, 218 new Class [] { 219 ClassLoader .class, 220 String .class, 221 AgentProfile.class, 222 MAFAgentSystem.class }) 223 .invoke( 224 null, 225 new Object [] { 226 parent, 227 codebase, 228 profile, 229 provider}); 230 } 231 catch (Throwable ex) 232 { 233 throw new Error ( 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 [] codebase2URLs(String codebase) 249 throws MalformedURLException 250 { 251 StringTokenizer parser = new StringTokenizer (codebase, " "); 252 URL [] urls = new URL [parser.countTokens()]; 253 int i=0; 254 while (parser.hasMoreTokens()) 255 { 256 urls[i++] = new URL (parser.nextToken()); 257 } 258 return urls; 259 } 260 } 261 | Popular Tags |