/** * The dependent service is available and the bundle will start. * * @param dependentService the UIService this activator is waiting. */ @Override public void start(Object dependentService) { if (logger.isDebugEnabled()) logger.debug("Update checker [STARTED]"); ConfigurationService cfg = getConfiguration(); if (OSUtils.IS_WINDOWS) { updateService = new Update(); bundleContext.registerService(UpdateService.class.getName(), updateService, null); // Register the "Check for Updates" menu item if // the "Check for Updates" property isn't disabled. if (!cfg.getBoolean(CHECK_FOR_UPDATES_MENU_DISABLED_PROP, false)) { // Register the "Check for Updates" menu item. CheckForUpdatesMenuItemComponent checkForUpdatesMenuItemComponent = new CheckForUpdatesMenuItemComponent(Container.CONTAINER_HELP_MENU); Hashtable<String, String> toolsMenuFilter = new Hashtable<String, String>(); toolsMenuFilter.put(Container.CONTAINER_ID, Container.CONTAINER_HELP_MENU.getID()); bundleContext.registerService( PluginComponent.class.getName(), checkForUpdatesMenuItemComponent, toolsMenuFilter); } // Check for software update upon startup if enabled. if (cfg.getBoolean(UPDATE_ENABLED, true)) updateService.checkForUpdates(false); } if (cfg.getBoolean(CHECK_FOR_UPDATES_DAILY_ENABLED_PROP, false)) { logger.info("Scheduled update checking enabled"); // Schedule a "check for updates" task that will run once a day int hoursToWait = calcHoursToWait(); Runnable updateRunnable = new Runnable() { public void run() { logger.debug("Performing scheduled update check"); getUpdateService().checkForUpdates(false); } }; mUpdateExecutor = Executors.newSingleThreadScheduledExecutor(); mUpdateExecutor.scheduleAtFixedRate( updateRunnable, hoursToWait, 24 * 60 * 60, TimeUnit.SECONDS); } if (logger.isDebugEnabled()) logger.debug("Update checker [REGISTERED]"); }
/** Prepares the factory for bundle shutdown. */ public void stop() { if (logger.isTraceEnabled()) logger.trace("Preparing to stop all protocol providers of" + this); synchronized (registeredAccounts) { for (Enumeration<ServiceRegistration> registrations = registeredAccounts.elements(); registrations.hasMoreElements(); ) { ServiceRegistration reg = registrations.nextElement(); stop(reg); reg.unregister(); } registeredAccounts.clear(); } }
/** * Returns the ServiceReference for the protocol provider corresponding to the specified accountID * or null if the accountID is unknown. * * @param accountID the accountID of the protocol provider we'd like to get * @return a ServiceReference object to the protocol provider with the specified account id and * null if the account id is unknown to the provider factory. */ public ServiceReference getProviderForAccount(AccountID accountID) { ServiceRegistration registration; synchronized (registeredAccounts) { registration = registeredAccounts.get(accountID); } return (registration == null) ? null : registration.getReference(); }
/** * Returns all properties necessary for the intialization of the account with * <tt>accountPrefix</tt>. * * @param accountPrefix the prefix contained by all property names for the the account we'd like * to initialized * @return a Hashtable that can be used when creating the account in a protocol provider factory. */ private Hashtable<String, String> getAccountProperties(String accountPrefix) { Hashtable<String, String> table = new Hashtable<String, String>(); String userID = System.getProperty(accountPrefix + ProtocolProviderFactory.USER_ID, null); assertNotNull( "The system property named " + accountPrefix + ProtocolProviderFactory.USER_ID + " has to tontain a valid Jabber address that could be used during " + "SIP Communicator's tests.", userID); table.put(ProtocolProviderFactory.USER_ID, userID); String passwd = System.getProperty(accountPrefix + ProtocolProviderFactory.PASSWORD, null); assertNotNull( "The system property named " + accountPrefix + ProtocolProviderFactory.PASSWORD + " has to contain the password corresponding to the account " + "specified in " + accountPrefix + ProtocolProviderFactory.USER_ID, passwd); table.put(ProtocolProviderFactory.PASSWORD, passwd); String serverAddress = System.getProperty(accountPrefix + ProtocolProviderFactory.SERVER_ADDRESS, null); // optional if (serverAddress != null) table.put(ProtocolProviderFactory.SERVER_ADDRESS, serverAddress); String serverPort = System.getProperty(accountPrefix + ProtocolProviderFactory.SERVER_PORT, null); // optional if (serverPort != null) table.put(ProtocolProviderFactory.SERVER_PORT, serverPort); return table; }
/** * Removes the specified account from the list of accounts that this provider factory is handling. * If the specified accountID is unknown to the ProtocolProviderFactory, the call has no effect * and false is returned. This method is persistent in nature and once called the account * corresponding to the specified ID will not be loaded during future runs of the project. * * @param accountID the ID of the account to remove. * @return true if an account with the specified ID existed and was removed and false otherwise. */ public boolean uninstallAccount(AccountID accountID) { // Unregister the protocol provider. ServiceReference serRef = getProviderForAccount(accountID); boolean wasAccountExisting = false; // If the protocol provider service is registered, first unregister the // service. if (serRef != null) { BundleContext bundleContext = getBundleContext(); ProtocolProviderService protocolProvider = (ProtocolProviderService) bundleContext.getService(serRef); try { protocolProvider.unregister(); } catch (OperationFailedException ex) { logger.error( "Failed to unregister protocol provider for account: " + accountID + " caused by: " + ex); } } ServiceRegistration registration; synchronized (registeredAccounts) { registration = registeredAccounts.remove(accountID); } // first remove the stored account so when PP is unregistered we can // distinguish between deleted or just disabled account wasAccountExisting = removeStoredAccount(accountID); if (registration != null) { // Kill the service. registration.unregister(); } return wasAccountExisting; }
/** * Creates a protocol provider for the given <tt>accountID</tt> and registers it in the bundle * context. This method has a persistent effect. Once created the resulting account will remain * installed until removed through the uninstallAccount method. * * @param accountID the account identifier * @return <tt>true</tt> if the account with the given <tt>accountID</tt> is successfully loaded, * otherwise returns <tt>false</tt> */ public boolean loadAccount(AccountID accountID) { // Need to obtain the original user id property, instead of calling // accountID.getUserID(), because this method could return a modified // version of the user id property. String userID = accountID.getAccountPropertyString(ProtocolProviderFactory.USER_ID); ProtocolProviderService service = createService(userID, accountID); Dictionary<String, String> properties = new Hashtable<String, String>(); properties.put(PROTOCOL, protocolName); properties.put(USER_ID, userID); ServiceRegistration serviceRegistration = bundleContext.registerService(ProtocolProviderService.class.getName(), service, properties); if (serviceRegistration == null) return false; else { synchronized (registeredAccounts) { registeredAccounts.put(accountID, serviceRegistration); } return true; } }
/** * Unloads the account corresponding to the given <tt>accountID</tt>. Unregisters the * corresponding protocol provider, but keeps the account in contrast to the uninstallAccount * method. * * @param accountID the account identifier * @return true if an account with the specified ID existed and was unloaded and false otherwise. */ public boolean unloadAccount(AccountID accountID) { // Unregister the protocol provider. ServiceReference serRef = getProviderForAccount(accountID); if (serRef == null) { return false; } BundleContext bundleContext = getBundleContext(); ProtocolProviderService protocolProvider = (ProtocolProviderService) bundleContext.getService(serRef); try { protocolProvider.unregister(); } catch (OperationFailedException ex) { logger.error( "Failed to unregister protocol provider for account : " + accountID + " caused by: " + ex); } ServiceRegistration registration; synchronized (registeredAccounts) { registration = registeredAccounts.remove(accountID); } if (registration == null) { return false; } // Kill the service. registration.unregister(); return true; }
/** * Returns a copy of the list containing the <tt>AccountID</tt>s of all accounts currently * registered in this protocol provider. * * @return a copy of the list containing the <tt>AccountID</tt>s of all accounts currently * registered in this protocol provider. */ public ArrayList<AccountID> getRegisteredAccounts() { synchronized (registeredAccounts) { return new ArrayList<AccountID>(registeredAccounts.keySet()); } }
/** Installs an account and verifies whether the installation has gone well. */ public void testInstallAccount() { // first obtain a reference to the provider factory ServiceReference[] serRefs = null; String osgiFilter = "(" + ProtocolProviderFactory.PROTOCOL + "=" + ProtocolNames.JABBER + ")"; try { serRefs = JabberSlickFixture.bc.getServiceReferences( ProtocolProviderFactory.class.getName(), osgiFilter); } catch (InvalidSyntaxException ex) { // this really shouldhn't occur as the filter expression is static. fail(osgiFilter + " is not a valid osgi filter"); } assertTrue( "Failed to find a provider factory service for protocol Jabber", serRefs != null && serRefs.length > 0); // Enable always trust mode for testing tls jabber connections ServiceReference confReference = JabberSlickFixture.bc.getServiceReference(ConfigurationService.class.getName()); ConfigurationService configurationService = (ConfigurationService) JabberSlickFixture.bc.getService(confReference); configurationService.setProperty(CertificateService.PNAME_ALWAYS_TRUST, Boolean.TRUE); // Keep the reference for later usage. ProtocolProviderFactory jabberProviderFactory = (ProtocolProviderFactory) JabberSlickFixture.bc.getService(serRefs[0]); // make sure the account is empty assertTrue( "There was an account registered with the account mananger " + "before we've installed any", jabberProviderFactory.getRegisteredAccounts().size() == 0); // Prepare the properties of the first jabber account. Hashtable<String, String> jabberAccount1Properties = getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_1_PREFIX); Hashtable<String, String> jabberAccount2Properties = getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_2_PREFIX); Hashtable<String, String> jabberAccount3Properties = getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_3_PREFIX); // try to install an account with a null account id try { jabberProviderFactory.installAccount(null, jabberAccount1Properties); fail( "installing an account with a null account id must result " + "in a NullPointerException"); } catch (NullPointerException exc) { // that's what had to happen } // now really install the accounts jabberProviderFactory.installAccount( jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount1Properties); jabberProviderFactory.installAccount( jabberAccount2Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount2Properties); jabberProviderFactory.installAccount( jabberAccount3Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount3Properties); // try to install one of the accounts one more time and verify that an // excepion is thrown. try { jabberProviderFactory.installAccount( jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount1Properties); fail( "An IllegalStateException must be thrown when trying to " + "install a duplicate account"); } catch (IllegalStateException exc) { // that's what supposed to happen. } // Verify that the provider factory is aware of our installation assertTrue( "The newly installed account was not in the acc man's " + "registered accounts!", jabberProviderFactory.getRegisteredAccounts().size() == 3); // Verify protocol providers corresponding to the new account have // been properly registered with the osgi framework. osgiFilter = "(&(" + ProtocolProviderFactory.PROTOCOL + "=" + ProtocolNames.JABBER + ")" + "(" + ProtocolProviderFactory.USER_ID + "=" + jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID) + "))"; try { serRefs = JabberSlickFixture.bc.getServiceReferences( ProtocolProviderService.class.getName(), osgiFilter); } catch (InvalidSyntaxException ex) { // this really shouldhn't occur as the filter expression is static. fail(osgiFilter + "is not a valid osgi filter"); } assertTrue( "An protocol provider was apparently not installed as " + "requested.", serRefs != null && serRefs.length > 0); Object jabberProtocolProvider = JabberSlickFixture.bc.getService(serRefs[0]); assertTrue( "The installed protocol provider does not implement " + "the protocol provider service.", jabberProtocolProvider instanceof ProtocolProviderService); }