View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.util;
16  
17  import org.figure8.join.core.setup.BootstrapUtil;
18  import org.figure8.join.core.ContainerContextHandler;
19  import org.figure8.join.core.setup.BootstrapManager;
20  import org.figure8.join.services.remoting.ScriptLogAccessService;
21  import org.figure8.join.services.remoting.beans.RemoteScriptLogInfo;
22  import org.figure8.join.services.scripting.ScriptLogAccessor;
23  import org.figure8.join.services.scripting.ScriptLogInfo;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.beanutils.PropertyUtils;
27  import com.caucho.hessian.client.HessianProxyFactory;
28  
29  import java.util.List;
30  import java.util.ArrayList;
31  /**
32   * This is a simple proxy for accessing script logs information whether
33   * the accessor is local or remote (in case of exploded deployment mode).
34   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35   * @version $Revision: 1.1 $
36   */
37  public class ScriptLogAccessorProxy{
38  
39     // Static -------------------------------------------------------------------
40  
41     /** Get a commons logger */
42     private static final Log log = LogUtil.getLog(ScriptLogAccessorProxy.class);
43  
44  
45     // Attributes ---------------------------------------------------------------
46  
47     /** Singleton own instance */
48     private static ScriptLogAccessorProxy me = new ScriptLogAccessorProxy();
49  
50     /** The script log accessor to proxy */
51     private ScriptLogAccessor manager = null;
52     /** The script log access service to proxy */
53     private ScriptLogAccessService service = null;
54  
55  
56     // Public -------------------------------------------------------------------
57  
58     /**
59      * Factory method to retrieve a proxy
60      * @return A ScriptLogAccessorProcy instance
61      */
62     public static ScriptLogAccessorProxy getInstance(){
63        return me;
64     }
65  
66     /**
67      * Retrieve all the info beans on available script logs for given entity
68      * @param entityId The id of entity to retrieve ScriptLogInfo objects for
69      * @param entityClass The class of entity to retrieve ScriptLogInfo objects for
70      * @return A list of {@link org.figure8.join.services.scripting.ScriptLogInfo} available for entity
71      */
72     public List getScriptLogInfos(long entityId, String entityClass){
73        // Prepare an empty result.
74        List result = new ArrayList(0);
75        try{
76           // Try initializing proxied objects.
77           initializeManagerOrService();
78           // Use manager or service ...
79           if (manager != null)
80              result = manager.getScriptLogInfos(entityId, entityClass);
81           else if (service != null){
82              RemoteScriptLogInfo[] remoteInfos = service.getScriptLogInfos(entityId, entityClass);
83              result = getLocalObjects(remoteInfos);
84           }
85        }
86        catch (Exception e){
87           // Reset service & manager.
88           service = null;
89           manager = null;
90        }
91        return result;
92     }
93  
94     /**
95      * Get all the info beans on available script logs for script having this name and category
96      * @param scriptName The name of the script to retrieve log for
97      * @param scriptCategory The category of the script to retrive log for
98      * @return A list of {@link org.figure8.join.services.scripting.ScriptLogInfo} available for script
99      */
100    public List getScriptLogInfos(String scriptName, String scriptCategory){
101       // Prepare an empty result.
102       List result = new ArrayList(0);
103       try{
104          // Try initializing proxied objects.
105          initializeManagerOrService();
106          // Use manager or service ...
107          if (manager != null)
108             result = manager.getScriptLogInfos(scriptName, scriptCategory);
109          else if (service != null){
110             RemoteScriptLogInfo[] remoteInfos = service.getScriptLogInfos(scriptName, scriptCategory);
111             result = getLocalObjects(remoteInfos);
112          }
113       }
114       catch (Exception e){
115          // Reset service & manager.
116          service = null;
117          manager = null;
118       }
119       return result;
120    }
121 
122 
123    // Protected ----------------------------------------------------------------
124 
125    /**
126     * Initialize the local consumer manager or the remote service depending
127     * on the application setup and the application side we are on.
128     * @throws Exception if manager cannot be found or remote service is not available
129     */
130    protected void initializeManagerOrService() throws Exception{
131       if (manager == null && service == null){
132          BootstrapManager bootstrapManager = BootstrapUtil.getBootstrapManager();
133          // Use remote service only if we are on synchronous side of a dissociate setup.
134          if (bootstrapManager.isDissociatedSetup() && bootstrapManager.isSynchronousSide()){
135             String url = bootstrapManager.getOtherSideUrl() + "/remoting/hessian/ScriptLogAccessService";
136             log.info("Connecting to remote ScriptLogAccessService using url: " + url);
137             HessianProxyFactory proxyFactory = new HessianProxyFactory();
138             service = (ScriptLogAccessService)proxyFactory.create(ScriptLogAccessService.class, "");
139          }
140          else{
141             // Use local manager provided by container facility.
142             log.info("Looking up local ScriptLogAccessor using 'scriptLogAccessor' component");
143             ContainerContextHandler handler = ContainerContextHandler.getInstance();
144             manager = (ScriptLogAccessor)handler.getComponent("scriptLogAccessor");
145          }
146       }
147    }
148 
149    /** Convert an array of remote object into a list of local domain object */ 
150    protected List getLocalObjects(RemoteScriptLogInfo[] remoteInfos) throws Exception{
151       List result = new ArrayList(remoteInfos.length);
152       for (int i=0; i<remoteInfos.length; i++){
153          RemoteScriptLogInfo remoteInfo = remoteInfos[i];
154          ScriptLogInfo info = new ScriptLogInfo();
155          PropertyUtils.copyProperties(info, remoteInfo);
156          result.add(info);
157       }
158       return result;
159    }
160 }