1 29 38 39 package org.planetamessenger.mos.engine; 40 41 import org.planetamessenger.security.*; 42 import org.planetamessenger.plugin.*; 43 import org.planetamessenger.io.*; 44 import org.planetamessenger.mos.forms.*; 45 import java.util.*; 46 import java.util.jar.*; 47 import java.io.*; 48 49 50 public class JPluginEngine { 51 52 public static final int NULL_PLUGIN_OBJECT = -1; 53 public static final int INCOMPATIBLE_PLUGIN_SPECIFICATION = -2; 54 55 JPluginEngineListener pluginEngineListener = null; 56 JKernelAPIListener kernelAPIListener = null; 57 HashMap<Integer , JPlugin> hPluginList = null; 58 HashMap<Integer ,JMOSSendMessageDlg> hMessageWindowList = null; 59 60 61 62 65 public JPluginEngine() { 66 67 hPluginList = new HashMap<Integer , JPlugin>(); 68 hMessageWindowList = new HashMap<Integer , JMOSSendMessageDlg>(); 69 } 70 71 76 public void addPluginEngineListener( JPluginEngineListener listener ) { 77 78 pluginEngineListener = listener; 79 } 80 81 86 public void removePluginEngineListener( JPluginEngineListener listener ) { 87 88 pluginEngineListener = null; 89 } 90 91 95 public void addKernelAPIListener( JKernelAPIListener listener ) { 96 97 kernelAPIListener = listener; 98 } 99 100 104 public void removeKernelAPIListener( JKernelAPIListener listener ) { 105 106 kernelAPIListener = null; 107 } 108 109 115 public int add( JPlugin plugin ) { 116 117 if( plugin != null ) { 118 if( plugin.getPluginProperties().getLibrarySpecificationMajorVersion() != JKernelConstants.MAJOR_LIBRARY_VERSION ) 119 return INCOMPATIBLE_PLUGIN_SPECIFICATION; 120 else { 121 boolean bIsPluginAvailable = JSharedObjects.getProfileManager().checkPluginInstall( plugin.getPluginProperties().getPluginId() ); 122 123 if( bIsPluginAvailable ) { 124 Integer iPluginId = new Integer ( plugin.getPluginProperties().getPluginId() ); 125 int nPluginListSize = hPluginList.values().toArray().length; 126 JPluginPreferencesContainer prefs = null; 127 128 for( int nCount = 0; nCount < nPluginListSize; nCount++ ) 130 if( hPluginList.values().toArray()[nCount].getClass().getName().compareTo( plugin.getClass().getName() ) == 0 ) 131 return -1; 132 133 plugin.addPluginEngineListener( JSharedObjects.getPluginEngineListener() ); 134 plugin.addKernelAPIListener( JSharedObjects.getKernelManager() ); 135 prefs = ( JPluginPreferencesContainer ) plugin.getPreferencesContainer(); 136 hPluginList.put( iPluginId, plugin ); 137 hMessageWindowList.put( iPluginId, new JMOSSendMessageDlg() ); 138 139 if( prefs != null ) 141 JSharedObjects.getPluginPreferencesManager().add( iPluginId.intValue(), prefs ); 142 143 return iPluginId.intValue(); 144 } 145 } 146 } 147 148 return NULL_PLUGIN_OBJECT; 149 } 150 151 155 public void remove( JPlugin plugin ) { 156 157 Integer iPluginId = new Integer ( plugin.getPluginProperties().getPluginId() ); 158 159 160 hPluginList.remove( iPluginId ); 161 hMessageWindowList.remove( iPluginId ); 162 163 if( pluginEngineListener != null ) 164 pluginEngineListener.onDestroyPlugin( plugin ); 165 166 try { 167 plugin.fireOnDestroy(); 168 } catch( Exception e ) { 169 System.err.println( "JPluginEngine.remove() - " + e ); 170 } 171 172 JSharedObjects.getPluginPreferencesManager().remove( iPluginId.intValue() ); 173 plugin = null; 174 } 175 176 182 public void removeAll() { 183 184 int nPluginListSize = hPluginList.values().toArray().length; 185 186 187 for( int nCount = 0; nCount < nPluginListSize; nCount++ ) { 188 org.planetamessenger.plugin.JPlugin plugin = ( org.planetamessenger.plugin.JPlugin ) hPluginList.values().toArray()[nCount]; 189 190 try { 191 plugin.fireOnDestroy(); 192 } catch( Exception e ) { 193 System.err.println( "JPluginEngine.remove() - " + e ); 194 } 195 196 plugin = null; 197 } 198 199 hPluginList.clear(); 200 hMessageWindowList.clear(); 201 } 202 203 209 public JPlugin get( int nPluginId ) { 210 211 Integer iPluginId = new Integer ( nPluginId ); 212 JPlugin plugin = ( JPlugin ) hPluginList.get( iPluginId ); 213 214 215 return plugin; 216 } 217 218 224 public JMOSSendMessageDlg getMessageWindow( int nPluginId ) { 225 226 Integer iPluginId = new Integer ( nPluginId ); 227 JMOSSendMessageDlg msgDlg = ( JMOSSendMessageDlg ) hMessageWindowList.get( iPluginId ); 228 229 return msgDlg; 230 } 231 232 239 public Object [] getAllMessageWindows() { 240 241 return hMessageWindowList.values().toArray(); 242 } 243 244 public JPlugin[] toArray() { 245 248 249 Object [] objectArray = hPluginList.values().toArray(); 250 251 252 if( objectArray.length == 0 ) 253 return null; 254 255 JPlugin[] pluginArray = new JPlugin[objectArray.length]; 256 257 for( int nCount = 0; nCount < objectArray.length; nCount++ ) 258 pluginArray[nCount] = ( JPlugin ) objectArray[nCount]; 259 260 return pluginArray; 261 } 262 263 266 public void destroy() { 267 268 removeAll(); 269 } 270 271 275 public JPlugin loadPlugin( String strJarFile ) { 276 277 try { 278 JPlugin plugin = null; 279 JJarClassLoader jarLoader = new JJarClassLoader(); 280 Manifest manifest = jarLoader.getManifestFromFile( strJarFile ); 281 282 283 String strDependency = manifest.getMainAttributes().getValue( "Depends" ); 285 286 if( strDependency != null ) { 287 StringTokenizer parser = new StringTokenizer( strDependency, ";" ); 288 289 try { 290 while( parser.hasMoreTokens() ) { 291 jarLoader.addDependency( JSharedObjects.getConfiguration().getDepLibPath() + parser.nextToken() ); 292 } 293 } catch( java.util.NoSuchElementException e ) { 294 System.err.println( "JPluginEngine.loadPlugin() - " + e ); 295 } 296 } 297 298 jarLoader.open( strJarFile ); 300 301 if( manifest != null ) { 302 303 String strEntryPointClass = manifest.getMainAttributes().getValue( "PluginEntryPoint" ); 304 305 306 if( strEntryPointClass != null ) { 307 308 int nPluginId = JSharedObjects.getProfileManager().getPluginId( strEntryPointClass ); 309 310 311 if( nPluginId != -1 ) { 312 boolean bIsPluginInstalled = ( hPluginList.get( new Integer ( nPluginId ) ) != null ? true : false ); 313 boolean bIsPluginAvailable = JSharedObjects.getProfileManager().checkPluginInstall( nPluginId ); 314 315 316 if( bIsPluginAvailable && !bIsPluginInstalled ) { 317 318 jarLoader.load(); 319 320 try { 321 String strPluginName = manifest.getMainAttributes().getValue( "PluginName" ); 322 String strPluginVersion = manifest.getMainAttributes().getValue( "Version" ); 323 String strPluginDescription = manifest.getMainAttributes().getValue( "Description" ); 324 String strPluginSpecMajorVersion = manifest.getMainAttributes().getValue( "PluginSpecMajorVersion" ); 325 String strPluginSpecMinorVersion = manifest.getMainAttributes().getValue( "PluginSpecMinorVersion" ); 326 String strPluginOwner = manifest.getMainAttributes().getValue( "Owner" ); 327 String strLicense = manifest.getMainAttributes().getValue( "License" ); 328 String strHomePage = manifest.getMainAttributes().getValue( "HomePage" ); 329 JPluginProperties pluginProperties = new JPluginProperties(); 330 331 332 pluginProperties.setPluginId( nPluginId ); 333 pluginProperties.setName( strPluginName ); 334 pluginProperties.setVersion( strPluginVersion ); 335 pluginProperties.setDescription( ( strPluginDescription == null ? "Default plugin implementation" : strPluginDescription ) ); 336 pluginProperties.setFileName( strJarFile ); 337 pluginProperties.setClassName( strEntryPointClass ); 338 pluginProperties.setPluginOwner( strPluginOwner ); 339 pluginProperties.setLicense( strLicense ); 340 pluginProperties.setHomePage( strHomePage ); 341 342 try { 343 pluginProperties.setLibrarySpecificationMinorVersion( Integer.parseInt( strPluginSpecMinorVersion ) ); 344 pluginProperties.setLibrarySpecificationMajorVersion( Integer.parseInt( strPluginSpecMajorVersion ) ); 345 } catch( NumberFormatException e ) { 346 System.err.println( "JPluginEngine.loadPlugin() - Invalid Plugin specification version" ); 347 } 348 349 if( pluginProperties.getLibrarySpecificationMajorVersion() == JKernelConstants.MAJOR_LIBRARY_VERSION ) { 351 plugin = ( JPlugin ) jarLoader.loadClass( strEntryPointClass ).newInstance(); 352 plugin.fireOnCreate( pluginProperties ); 353 add( plugin ); 354 355 if( pluginEngineListener != null ) 357 pluginEngineListener.onCreatePlugin( plugin ); 358 } 359 else { 360 System.err.println( "JPluginEngine.loadPlugin() - " + pluginProperties.getName() + " was built with incompatible plugin library specification" ); 361 362 if( pluginEngineListener != null ) 364 pluginEngineListener.onError( pluginProperties.getPluginId(), pluginProperties.getName(), JPluginEngineListener.ERROR_INCOMPATIBLE_PLUGIN ); 365 } 366 } catch( ClassNotFoundException ce ) { 367 System.err.println( "JPluginEngine.loadPlugin() - " + ce ); 368 } catch( Exception e ) { 369 System.err.println( "JPluginEngine.loadPlugin() - " + e ); 370 } 371 } 372 } 373 } 374 else 375 System.err.println( "JPluginEngine.loadPlugin() - Unable to load the plugin. No entry point defined in Manifest file (PluginEntryPoint:)" ); 376 } 377 378 return plugin; 379 380 } catch( JarException e ) { 381 System.err.println( "JPluginEngine.loadPlugin()" + e ); 382 return null; 383 } 384 } 385 386 393 public boolean loadAllPlugins( long nProfileId, boolean bStartPlugin ) { 394 395 java.lang.String [] strPlugins; 396 File pluginList = new File( JSharedObjects.getConfiguration().getPluginsPath() ); 397 JFilenameFilter pluginFilter = new JFilenameFilter(); 398 int nPluginListSize; 399 400 401 pluginFilter.add( "jar" ); 403 strPlugins = pluginList.list( pluginFilter ); 404 nPluginListSize = strPlugins.length; 405 406 if( bStartPlugin ) 408 JSharedObjects.getProfileManager().setProfileId( nProfileId ); 409 410 if( nPluginListSize == 0 ) 412 return true; 413 414 for( int nCount = 0; nCount < nPluginListSize; nCount++ ) { 415 416 JPlugin plugin; 417 418 419 if( !bStartPlugin ) { 421 updateNewPlugin( JSharedObjects.getConfiguration().getPluginsPath() + strPlugins[nCount] ); 422 continue; 423 } 424 425 plugin = loadPlugin( JSharedObjects.getConfiguration().getPluginsPath() + strPlugins[nCount] ); 426 427 if( plugin != null ) { 428 java.lang.Object [] contactList = JSharedObjects.getContactListManager().getContactList( plugin ).values().toArray(); 429 JUserInfo userInfo = JSharedObjects.getProfileManager().getUserInfo( plugin.getPluginProperties().getPluginId() ); 430 431 432 if( userInfo != null ) { 434 org.planetamessenger.plugin.JContactListItem item = null; 435 436 437 for( int nItems = 0; nItems < contactList.length; nItems++ ) { 439 item = ( org.planetamessenger.plugin.JContactListItem ) contactList[nItems]; 440 441 if( item.getContactFlag() == JContactListItem.CONTACT_NOT_IN_LIST ) { 443 if( JSharedObjects.getContactListManager().countContactMessages( item ) > 0 ) 444 item.setStatus( JPlugin.STATUS_NOT_IN_LIST ); 445 else { 446 JSharedObjects.getContactListManager().removeFromContactList( plugin, item.getUserId() ); 447 continue; 448 } 449 } 450 451 JSharedObjects.getMainWindow().getContactList().addItem( item ); 452 } 453 454 plugin.setUserInfo( userInfo ); 455 456 if( userInfo.getAutoConnect() == 1 ) { 458 try { 459 plugin.fireOnLogin(); 460 } catch( Exception e ) { 461 System.err.println( "JPluginEngine.loadAllPlugins() - " + e ); 462 } 463 } 464 } 465 } 466 } 467 468 return true; 469 } 470 471 476 public JPluginProperties getPluginProperties( String strJarFile ) { 477 478 try { 479 JPluginProperties pluginProperties = null; 480 JJarClassLoader jarLoader = new JJarClassLoader( strJarFile, false ); 481 Manifest manifest = jarLoader.getManifest(); 482 483 484 if( manifest != null ) { 485 486 pluginProperties = new JPluginProperties(); 487 488 pluginProperties.setName( manifest.getMainAttributes().getValue( "PluginName" ) ); 489 pluginProperties.setVersion( manifest.getMainAttributes().getValue( "Version" ) ); 490 pluginProperties.setDescription( manifest.getMainAttributes().getValue( "Description" ) ); 491 pluginProperties.setClassName( manifest.getMainAttributes().getValue( "PluginEntryPoint" ) ); 492 pluginProperties.setLicense( manifest.getMainAttributes().getValue( "License" ) ); 493 pluginProperties.setPluginOwner( manifest.getMainAttributes().getValue( "Owner" ) ); 494 pluginProperties.setHomePage( manifest.getMainAttributes().getValue( "HomePage" ) ); 495 pluginProperties.setFileName( strJarFile ); 496 497 try { 498 pluginProperties.setLibrarySpecificationMinorVersion( Integer.parseInt( manifest.getMainAttributes().getValue( "PluginSpecMinorVersion" ) ) ); 499 pluginProperties.setLibrarySpecificationMajorVersion( Integer.parseInt( manifest.getMainAttributes().getValue( "PluginSpecMajorVersion" ) ) ); 500 } catch( NumberFormatException e ) { 501 System.err.println( "JPluginEngine.getPluginProperties() - Invalid Plugin specification version" ); 502 } 503 } 504 505 return pluginProperties; 506 507 } catch( JarException e ) { 508 System.err.println( "JPluginEngine.getPluginProperties()" + e ); 509 return null; 510 } 511 } 512 513 517 public JPluginProperties getPluginPropertiesByClassName( String strClassName ) { 518 519 if( strClassName != null ) { 520 521 java.lang.String [] strPlugins; 522 JPluginProperties pluginProperties = null; 523 java.io.File pluginList = new File( JSharedObjects.getConfiguration().getPluginsPath() ); 524 JFilenameFilter pluginFilter = new JFilenameFilter(); 525 int nPluginListSize; 526 527 528 pluginFilter.add( "jar" ); 530 strPlugins = pluginList.list( pluginFilter ); 531 nPluginListSize = strPlugins.length; 532 533 if( nPluginListSize == 0 ) 535 return null; 536 537 for( int nCount = 0; nCount < nPluginListSize; nCount++ ) { 538 539 pluginProperties = getPluginProperties( JSharedObjects.getConfiguration().getPluginsPath() + strPlugins[nCount] ); 540 541 if( strClassName.compareTo( pluginProperties.getClassName() ) == 0 ) 542 return pluginProperties; 543 } 544 } 545 546 return null; 547 } 548 549 552 public int getPluginCount() { 553 554 return hPluginList.size(); 555 } 556 557 565 public boolean onSendMessage( org.planetamessenger.plugin.JContactListItem selectedItem, java.lang.String msg ) { 566 567 JPlugin plugin = get( selectedItem.getPluginId() ); 568 569 if( plugin != null ) { 570 if( plugin.isConnected() ) { 571 try { 572 return plugin.fireOnSendMessage( selectedItem.getUserId(), msg ); 573 } catch( Exception e ) { 574 System.err.println( "JPluginEngine.onSendMessage() - " + e ); 575 return false; 576 } 577 } 578 else { 579 if( pluginEngineListener != null ) 581 pluginEngineListener.onError( plugin.getPluginProperties().getPluginId(), plugin.getPluginProperties().getName(), JPluginEngineListener.ERROR_DISCONNECTED_PLUGIN ); 582 } 583 } 584 585 return false; 586 } 587 588 594 private void updateNewPlugin( String strJarFile ) { 595 596 try { 597 JPlugin plugin = null; 598 JJarClassLoader jarLoader = new JJarClassLoader(); 599 Manifest manifest = jarLoader.getManifestFromFile( strJarFile ); 600 601 jarLoader.open( strJarFile ); 603 604 if( manifest != null ) { 605 String strEntryPointClass = manifest.getMainAttributes().getValue( "PluginEntryPoint" ); 606 607 if( strEntryPointClass != null ) 608 JSharedObjects.getProfileManager().getPluginId( strEntryPointClass ); 609 } 610 611 jarLoader.close(); 612 jarLoader = null; 613 614 } catch( JarException e ) { 615 System.err.println( "JPluginEngine.updateNewPlugin()" + e ); 616 } 617 } 618 } 619 | Popular Tags |