1 36 package org.ungoverned.moduleloader; 37 38 import java.util.*; 39 40 117 public class ModuleManager 118 { 119 private List m_moduleList = new ArrayList(); 120 private Map m_moduleMap = new HashMap(); 121 private SearchPolicy m_searchPolicy = null; 122 private URLPolicy m_urlPolicy = null; 123 private ModuleListener[] m_listeners = null; 124 private static final ModuleListener[] m_noListeners = new ModuleListener[0]; 125 126 134 public ModuleManager(SearchPolicy searchPolicy) 135 { 136 this(searchPolicy, null); 137 } 138 139 149 public ModuleManager(SearchPolicy searchPolicy, URLPolicy urlPolicy) 150 { 151 m_listeners = m_noListeners; 152 m_searchPolicy = searchPolicy; 153 m_searchPolicy.setModuleManager(this); 154 155 if (urlPolicy == null) 156 { 157 m_urlPolicy = new DefaultURLPolicy(); 158 } 159 else 160 { 161 m_urlPolicy = urlPolicy; 162 } 163 } 164 165 172 public URLPolicy getURLPolicy() 173 { 174 return m_urlPolicy; 175 } 176 177 184 public SearchPolicy getSearchPolicy() 185 { 186 return m_searchPolicy; 187 } 188 189 200 public synchronized Module[] getModules() 201 { 202 Module[] modules = new Module[m_moduleList.size()]; 203 return (Module[]) m_moduleList.toArray(modules); 204 } 205 206 214 public synchronized Module getModule(String id) 215 { 216 return (Module) m_moduleMap.get(id); 217 } 218 219 240 public Module addModule(String id, Object [][] attributes, 241 ResourceSource[] resSources, LibrarySource[] libSources) 242 { 243 Module module = null; 244 245 synchronized (this) 248 { 249 if (m_moduleMap.get(id) == null) 250 { 251 module = new Module(this, id, attributes, resSources, libSources); 252 m_moduleList.add(module); 253 m_moduleMap.put(id, module); 254 } 255 else 256 { 257 throw new IllegalArgumentException ("Module ID must be unique."); 258 } 259 } 260 261 fireModuleAdded(module); 263 264 return module; 265 } 266 267 300 public void resetModule( 301 Module module, Object [][] attributes, 302 ResourceSource[] resSources, LibrarySource[] libSources) 303 { 304 synchronized (this) 307 { 308 module = (Module) m_moduleMap.get(module.getId()); 309 if (module != null) 310 { 311 module.reset(attributes, resSources, libSources); 312 } 313 else 314 { 315 return; 317 } 318 } 319 320 fireModuleReset(module); 322 } 323 324 334 public void removeModule(Module module) 335 { 336 synchronized (this) 339 { 340 if (m_moduleMap.get(module.getId()) != null) 341 { 342 m_moduleList.remove(module); 344 m_moduleMap.remove(module.getId()); 345 346 module.dispose(); 348 } 349 else 350 { 351 return; 353 } 354 } 355 356 fireModuleRemoved(module); 358 } 359 360 367 public void addModuleListener(ModuleListener l) 368 { 369 if (l == null) 371 { 372 throw new IllegalArgumentException ("Listener is null"); 373 } 374 375 synchronized (m_noListeners) 377 { 378 if (m_listeners == m_noListeners) 380 { 381 m_listeners = new ModuleListener[] { l }; 382 } 383 else 389 { 390 ModuleListener[] newList = new ModuleListener[m_listeners.length + 1]; 391 System.arraycopy(m_listeners, 0, newList, 0, m_listeners.length); 392 newList[m_listeners.length] = l; 393 m_listeners = newList; 394 } 395 } 396 } 397 398 404 public void removeModuleListener(ModuleListener l) 405 { 406 if (l == null) 408 { 409 throw new IllegalArgumentException ("Listener is null"); 410 } 411 412 synchronized (m_noListeners) 414 { 415 int idx = -1; 417 for (int i = 0; i < m_listeners.length; i++) 418 { 419 if (m_listeners[i].equals(l)) 420 { 421 idx = i; 422 break; 423 } 424 } 425 426 if (idx >= 0) 428 { 429 if (m_listeners.length == 1) 431 { 432 m_listeners = m_noListeners; 433 } 434 else 440 { 441 ModuleListener[] newList = new ModuleListener[m_listeners.length - 1]; 442 System.arraycopy(m_listeners, 0, newList, 0, idx); 443 if (idx < newList.length) 444 { 445 System.arraycopy(m_listeners, idx + 1, newList, idx, 446 newList.length - idx); 447 } 448 m_listeners = newList; 449 } 450 } 451 } 452 } 453 454 461 protected void fireModuleAdded(Module module) 462 { 463 ModuleEvent event = null; 465 466 ModuleListener[] listeners = m_listeners; 469 470 for (int i = 0; i < listeners.length; i++) 472 { 473 if (event == null) 475 { 476 event = new ModuleEvent(this, module); 477 } 478 listeners[i].moduleAdded(event); 479 } 480 } 481 482 488 protected void fireModuleReset(Module module) 489 { 490 ModuleEvent event = null; 492 493 ModuleListener[] listeners = m_listeners; 496 497 for (int i = 0; i < listeners.length; i++) 499 { 500 if (event == null) 502 { 503 event = new ModuleEvent(this, module); 504 } 505 listeners[i].moduleReset(event); 506 } 507 } 508 509 516 protected void fireModuleRemoved(Module module) 517 { 518 ModuleEvent event = null; 520 521 ModuleListener[] listeners = m_listeners; 524 525 for (int i = 0; i < listeners.length; i++) 527 { 528 if (event == null) 530 { 531 event = new ModuleEvent(this, module); 532 } 533 listeners[i].moduleRemoved(event); 534 } 535 } 536 } | Popular Tags |