Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: s IT Solutions AT Spardat GmbH - initial API and implementation /
 
 
 package at.spardat.xma.boot.comp;
 
 import java.io.File;
 import java.net.URL;
 
The AppManager is responsible for handling the application-context and classloaders for xma-components that it also invokes.

Author(s):
s3595 Chris Sch�fer (CGS)
Version:
$Id: AppManager.java 10853 2013-06-28 12:02:20Z dschwarz $
 
 public class AppManager  {

    
singleton
 
     private static AppManager instance_;

    
logger
 
     private Logger log_;

    
running applications. contains an AppContainerfor each running application.
 
     private Hashtable apps_;

    
configuration properties
 
     private Properties props;

    
loads and checks application descriptions
 
     private AppLoader loader;

    
if 'boot.transport.hostname.accept' is set, checks in getComponent() if the host is to accept
 
     private HostnameVerifierImpl hostNameAccepter;

    
if 'boot.transport.hostname.block' is set, checks in getComponent() if the host is to block
 
     private HostnameVerifierImpl hostNameBlocker;

    
constructor
 
     public AppManagerProperties pnew ) {
              = pnew;
               = Logger.getLogger"boot.appManager"); //$NON-NLS-1$
              = new Hashtable(10);
 
        String acceptHosts = .getProperty(.);
        if(acceptHosts != null && acceptHosts.length() > 0)
             = new HostnameVerifierImpl (acceptHosts);
        String blockHosts = .getProperty(.);
        if(blockHosts != null && blockHosts.length() > 0)
             = new HostnameVerifierImpl (blockHosts);
        
        // initialize trust manager for pinning
        Pinning509TrustManager.getInstance();
     }

    
initialisation
 
     public static synchronized AppManager initializeProperties pnew ) {
          // if(instance_ == null )
             = new AppManagerpnew );
        return  ;
     }

    
get instance
 
    public static AppManager getInstance( ) {
        if( == null)
          throw new IllegalStateException"Application Manager not initialized"); //$NON-NLS-1$
        else
          return ;
    }

    
prepare, and instantiate a component.
at.spardat.xma.boot.Launcher.launch(java.lang.String,at.spardat.xma.boot.KeyValue[])

Throws:
java.lang.Exception unrecoverable exception.
    public IComponent getComponentString componentthrows Exception {
        return getComponent(component,new Properties());
    }

    
prepare, and instantiate a component.
at.spardat.xma.boot.Launcher.launch(java.lang.String,at.spardat.xma.boot.KeyValue[])

Throws:
java.lang.Exception unrecoverable exception.
    public IComponent getComponent(String componentProperties propertiesthrows Exception {
       XMA_URI uri       = new XMA_URI(component);
       if(!acceptXMA_URI(uri)){
           throw new BootRuntimeException(BRTCodes.getText(.), component /*uri.getComponent()*/,uri.getServer()).setCode(.);
       }
       String appname    = uri.getApplication();
       AppContainer appC = (AppContainer).get(appname);
       /* if this is the first component for this application,
        * prepare the application and session
        */
       ifappC == null ) {
           appC = initializeApplication(uri,properties) ;
       }
       /*
        * if this component is accessed the first time during this application start,
        * load the components resources into the classloader, and register it as running.
        */
       XMAComponent cmp = appC.getApp().getComponenturi.getComponent() );
       if(cmp==null)
       if( !appC.isRunningCmp(cmp) ) {
           if (.isFdm()) {
               .getFdmLoader().loadComponent(cmp);
               if (cmp.getFdmUrl() != null) {
                   appC.getCcl().addURL(cmp.getFdmUrl());
               }
           }
           /* load resources for this component */
           .loadResourcesuricmp.getRes(), appC.getServerVers());
           /* the classloader needs all component resources for a component to run,
            * but only once. */
           ArrayList array = getComponentUrlsuricmp..);
           for (int i = 0; i < array.size(); i++) {
               appC.getCcl().addURL( (URL)array.get(i) );
           }
           // add the native libs, too
           appC.addRunningCmp(cmp);
       }
        /*
         * load the component from the application classloader
         */
       Class c = appC.getCcl().loadClasscmp.getImplPackage_() + . + cmp.getName_() );
       Class sessionClass = appC.getCcl().loadClass"at.spardat.xma.session.XMASessionClient" ); //$NON-NLS-1$
       Constructor ccons = c.getConstructor(new Class[]{ sessionClass });
       Object rtc  = ccons.newInstancenew Object[] { appC.getSession() } );
       IComponentHelper compHelper = .getCompHelper();
       cmp.addInstance(compHelper.castToIComponent(rtc));
       return compHelper.castToIComponent(rtc);
}

   
This method tests if the given host is in the BootRuntime properties: 'boot.transport.hostname.accept' 'boot.transport.hostname.block' The entries in the two properties are seperated by '|' or ';' and every entry may contain one wildcard character ('*'). If 'boot.transport.hostname.accept' is empty then every host is accepted, otherwise the given host has to match an entry of this property. Even an accepted host can be blocked by an entry in 'boot.transport.hostname.block'. A hostname is also local DNS resoled to its IP adress, so you can also allow or block hosts by IP adresses. If a host is not acceptable then a BootRuntimeException is thrown. This method is in package scope to be used by AppManagerTest.

Parameters:
uri
Throws:
at.spardat.xma.boot.exc.BootRuntimeException
Author(s):
s3460
Since:
version_number
    boolean acceptXMA_URI(XMA_URI uri){
        String host = uri.getServer();
        if( != null && !.verify(host)){
            .log(."The host \"{0}\" is not accepted as it is not configured in \"{1}\" having value : {2}"new Object[]{host..getProperty(.)});
            return false;
        }
        if( != null && .verify(host)){
            .log(."The host \"{0}\" is blocked as it is configured in \"{1}\" having value: {2}"new Object[]{host..getProperty(.)});
            return false;
        }
        return true;
    }

   public Properties launchString componentProperties input)
      throws Exception {
        Properties output = new Properties();
        Object rtc = getComponent(component,input);
        output = .getCompHelper().launch(rtc,input);
        return output;
    }

    
cleanup components and application resources. logout from application, if this component is the last one for this application.

Parameters:
rtc the terminating component
    public void endComponent(IComponent rtc) {
        AppContainer appC   = .getCompHelper().getRTSession(rtc).getAppContainer();
        String strClassName = rtc.getClass().getName();
        String strCmpName   = strClassName.substring(strClassName.lastIndexOf('.')+1);
        XMAComponent cmp    = appC.getRunningCmpstrCmpName);
        cmp.remInstance(rtc);
        ifcmp.instanceCount() == 0 ) {
            XMAComponent cmpT    = appC.remRunningCmp(strCmpName);
            .log(."removed component: {0}"cmpT.getName_()); //$NON-NLS-1$
        }
        ifappC.runningCmp() == 0 ) {
            appC.getSession().logout();
            .removeappC.getApp().getName_() );
        }
    }

    
prepare urls for the classloader for this component.

Returns:
ArrayList array of component resource urls
    private ArrayList getComponentUrlsXMA_URI uriXMAComponent cmpString typethrows Exception {
        ArrayList array = new ArrayList(10);
        // for each resource
        for (Iterator iter = cmp.getRes().values().iterator(); iter.hasNext();) {
            XMAResource res = (XMAResourceiter.next();
            AppLoader.urlLocalToArray(res,array,type);
        }
       return array;
    }
    static class DigestServer {
        byte[] hash;
        VersionNumber version;
    }
        DigestServer digestServer = new DigestServer();
        uri.setComponent(null);
        uri.setResource("session");
        uri.addParameter("operation","checkVersion");
        try{
            byte[] answer = Transport.getTransport().callServerEvent(null,uri,new byte[0],true);
            if(answer.length<16) throw new RuntimeException("server digest to short: "+new String(answer));
            digestServer.hash = new byte[16];
            System.arraycopy(answer,0,digestServer.hash,0,16);
            if(answer.length>16) {
                try {
                    digestServer.version=VersionNumber.parse("_"+new String(answer,16,answer.length-16,"US-ASCII")+".");
                } catch (UnsupportedEncodingException uee) {
                    throw new RuntimeException("Encodung US-ASCII not supported ",uee);
                }
            }
            if(digestServer.version==null) {
                digestServer.version=new VersionNumber(0,0,0);
            }
            return digestServer;
        } catch (CommunicationException e) {
            Transport.getTransport().logProxyInfo(.);
            throw e;
        }
    }

    
compare to application descriptor hash codes
    private boolean comparebyte[] abyte[] b ) {
        ifa==null || b==null || a.length != b.length) {
            return false;
        }
        forint i=0; i<a.length;i++){
            if(a[i] != b[i])
                return false;
        }
        return true;
    }

    
load the application from file or server, check the server version and update if necessary. create the application runtime session, and call login

Parameters:
uri
Returns:
AppContainer
Throws:
java.lang.Exception
    private AppContainer initializeApplication(XMA_URI uri,Properties propertiesthrows Exception {
        AppContainer appC          = null;
        appC = loadAndUpdateApplicationuri );
        appC.setAppManager(this);
        // create SWT-Display
        /* create a applicaton session  */
        Class sessionClass          = appC.getCcl().loadClass"at.spardat.xma.session.XMASessionClient" ); //$NON-NLS-1$
        Constructor sessionConst    = sessionClass.getConstructor(new Class[]{ XMA_URI.classAppContainer.class });
        XMA_URI baseURI             = uri.getApplicationURI();
        /* performs client login within session constructor */
        IRtXMASessionClient session = (IRtXMASessionClientsessionConst.newInstance(new Object[] {baseURIappC});
        //@todo InvocationTargetException -> DetailException auspacken und weiterwerfen.
        appC.setSession(session);
        /* login */
        boolean loggedIn=false;
        try {
            loggedIn=session.login(properties);
        } catch (AbstractMethodError ame) { //compatibility with runtime before 1.7.0
            loggedIn=session.login();
        }
        if(!loggedIn) {
            .log."(server) login failed" );  //$NON-NLS-1$
            throw new BRLoginException("login failed");
        }
        /* add to list of running applications */
        .puturi.getApplication(), appC );
        /* mark application as used today (for cache cleanup only)*/
        return appC;
    }

    
Loads the given application from the cache and updates it from the server if neccessary.

Parameters:
uri of the application
Returns:
AppContainer containing the loaded application description
Throws:
at.spardat.xma.boot.transport.CommunicationException if the application hash can not be loaded from the server
at.spardat.xma.boot.exc.BootRuntimeException if the download of the application resources failed twice
        AppContainer appC          = null;
        if == null = new AppLoader );
        .initFdm(uri.getServer());
        DigestServer digest = getDigestServer(uri.getApplicationURI());
        
        XMA_URI redirection = Transport.getTransport().getRedirection(uri);
        if (redirection != null) {
            // fill existing uri with redirect location
            XMA_URI.copyURI(uriredirection);
        }
        
        /* load the application */
        try {
            appC = .loadApplicationuri.getApplicationURI(), digest.hashdigest.version );
        } catch (Exception exc) { // download failed -> clear cache and retry
            .log(.,"application loading failed: ",exc);
            .log(.,"retrying loading of application ...");
            try {
                Util util = Util.getInstance();
                String dirName = util.urlToPath(uri.getApplicationURI().getHTTP_URI()).toString();
                File dir = new File(.getFileCache().getBaseDir(),dirName);
                util.removeApplicationRecursivedir );
                appC = .loadApplication(uri,digest.hashdigest.version);
            } catch (Exception e2) {
                .log(.,"application loading failed twice: ",e2);
            }
        }
        Pinning509TrustManager.getInstance().checkCertificatePinning(appC.getApp());
        
        return appC;
    }

    
Returns the classloader used for loading SWT classes. This method just calls AppLoader.getSwtClassLoader()

Returns:
the swtClassloader or null if no AppLoader or no swtClassloader allready exists.
    public CCLoader getSWTClassLoader() {
        return !=null?.getSwtClassLoader():null;
    }

    
Returns the ComponentHelper used for SWT-dependent methods. This method just calls AppLoader.getCompHelper()

Returns:
the ComponentHelper or null if no AppLoader or no ComponentHelper allready exists.
        return !=null?.getCompHelper():null;
    }
}// class
New to GrepCode? Check out our FAQ X