1 36 package org.ungoverned.moduleloader; 37 38 import java.security.AccessController ; 39 import java.security.PrivilegedAction ; 40 import java.util.*; 41 42 96 public class Module 97 { 98 102 public static final int KEY_IDX = 0; 103 107 public static final int VALUE_IDX = 1; 108 109 private ModuleManager m_mgr = null; 110 private String m_id = null; 111 private Map m_attributeMap = new HashMap(); 112 private ResourceSource[] m_resSources = null; 113 private LibrarySource[] m_libSources = null; 114 private ModuleClassLoader m_loader = null; 115 116 136 public Module( 137 ModuleManager mgr, String id, Object [][] attributes, 138 ResourceSource[] resSources, LibrarySource[] libSources) 139 { 140 m_mgr = mgr; 141 m_id = id; 142 initialize(attributes, resSources, libSources); 143 } 144 145 151 public String getId() 152 { 153 return m_id; 154 } 155 156 168 public synchronized Object [][] getAttributes() 169 { 170 Set s = m_attributeMap.entrySet(); 171 Object [][] attributes = new Object [s.size()][]; 172 Iterator iter = s.iterator(); 173 for (int i = 0; iter.hasNext(); i++) 174 { 175 Map.Entry entry = (Map.Entry) iter.next(); 176 attributes[i] = new Object [] { entry.getKey(), entry.getValue() }; 177 } 178 return attributes; 179 } 180 181 188 public synchronized Object getAttribute(String key) 189 { 190 return m_attributeMap.get(key); 191 } 192 193 201 public synchronized void setAttribute(String key, Object value) 202 { 203 m_attributeMap.put(key, value); 204 } 205 206 216 public ResourceSource[] getResourceSources() 217 { 218 return m_resSources; 219 } 220 221 231 public LibrarySource[] getLibrarySources() 232 { 233 return m_libSources; 234 } 235 236 245 public synchronized ModuleClassLoader getClassLoader() 246 { 247 if (m_loader == null) 248 { 249 if (System.getSecurityManager() != null) 250 { 251 m_loader = (ModuleClassLoader) AccessController.doPrivileged( 252 new GetClassLoaderPrivileged(m_mgr, this)); 253 } 254 else 255 { 256 m_loader = new ModuleClassLoader(m_mgr, this); 257 } 258 } 259 260 return m_loader; 261 } 262 263 269 public String toString() 270 { 271 return m_id; 272 } 273 274 286 protected synchronized void reset( 287 Object [][] attributes, ResourceSource[] resSources, 288 LibrarySource[] libSources) 289 { 290 m_loader = null; 292 m_attributeMap.clear(); 294 dispose(); 296 initialize(attributes, resSources, libSources); 298 } 299 300 305 protected synchronized void dispose() 306 { 307 for (int i = 0; (m_resSources != null) && (i < m_resSources.length); i++) 309 { 310 m_resSources[i].close(); 311 } 312 for (int i = 0; (m_libSources != null) && (i < m_libSources.length); i++) 313 { 314 m_libSources[i].close(); 315 } 316 } 317 318 329 private void initialize( 330 Object [][] attributes, ResourceSource[] resSources, LibrarySource[] libSources) 331 { 332 for (int i = 0; (attributes != null) && (i < attributes.length); i++) 333 { 334 m_attributeMap.put(attributes[i][KEY_IDX], attributes[i][VALUE_IDX]); 335 } 336 337 m_resSources = resSources; 338 m_libSources = libSources; 339 340 for (int i = 0; (m_resSources != null) && (i < m_resSources.length); i++) 342 { 343 m_resSources[i].open(); 344 } 345 for (int i = 0; (m_libSources != null) && (i < m_libSources.length); i++) 346 { 347 m_libSources[i].open(); 348 } 349 } 350 351 private static class GetClassLoaderPrivileged implements PrivilegedAction 352 { 353 private ModuleManager m_mgr = null; 354 private Module m_module = null; 355 356 public GetClassLoaderPrivileged(ModuleManager mgr, Module module) 357 { 358 m_mgr = mgr; 359 m_module = module; 360 } 361 362 public Object run() 363 { 364 return new ModuleClassLoader(m_mgr, m_module); 365 } 366 } 367 } | Popular Tags |