KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > instantiator > StatelessServantLocator


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.instantiator;
6
7 import java.util.Map JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.apache.commons.pool.ObjectPool;
12
13 import org.omg.CORBA.CompletionStatus JavaDoc;
14 import org.omg.CORBA.INTERNAL JavaDoc;
15
16 import org.omg.PortableServer.ForwardRequest JavaDoc;
17 import org.omg.PortableServer.POA JavaDoc;
18 import org.omg.PortableServer.Servant JavaDoc;
19 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder JavaDoc;
20
21 /**
22  * Stateless servant locator is invoked whenever a stateless object is invoked
23  * It's responsible for finding a servant to process the request
24  * @author Carlos Arévalo, Nelson Arapé
25  */

26 //public class StatelessServantLocator extends ServantLocatorPOA
27
public class StatelessServantLocator
28         extends org.omg.CORBA.LocalObject JavaDoc
29         implements org.omg.PortableServer.ServantLocator JavaDoc
30 {
31     private static final Log LOG = LogFactory.getLog(StatelessServantLocator.class);
32
33     private static final int ERR_MINOR_CODE = 0;
34     private static final Object JavaDoc ERROR_COOKIE = new int[0];
35
36     private Map JavaDoc objectInfoTable = null;
37
38     /**
39      * Class constructor.
40      * @param objectInfoTable a map that associates the object IDs with object's ObjectInfo
41      */

42     public StatelessServantLocator(Map JavaDoc objectInfoTable)
43     {
44         this.objectInfoTable = objectInfoTable;
45     }
46
47     /**
48      * @see org.omg.PortableServer.ServantLocatorOperations#preinvoke(byte[],
49      * org.omg.PortableServer.POA, java.lang.String, org.omg.PortableServer.ServantLocatorPackage.CookieHolder)
50      */

51     public Servant JavaDoc preinvoke(byte[] oid, POA JavaDoc adapter, String JavaDoc operation, CookieHolder JavaDoc theCookie)
52             throws ForwardRequest JavaDoc
53     {
54         try
55         {
56             if (LOG.isDebugEnabled()) LOG.debug("Preinvoke " + operation + " on " + new String JavaDoc(oid));
57             StatelessObjectInfo info = (StatelessObjectInfo) objectInfoTable.get(new String JavaDoc(oid));
58             ObjectPool pool = info.getServantPool();
59             Servant JavaDoc servant = (Servant JavaDoc) pool.borrowObject();
60             if (LOG.isDebugEnabled()) LOG.debug("Servant obtained from the pool");
61             return servant;
62         }
63         catch (Exception JavaDoc e)
64         {
65             if (LOG.isErrorEnabled())
66             {
67                 LOG.error(e.getMessage(), e);
68             }
69             theCookie.value = ERROR_COOKIE;
70             throw new INTERNAL JavaDoc(e.getMessage(), ERR_MINOR_CODE, CompletionStatus.COMPLETED_NO);
71         }
72     }
73
74     /**
75      * @see org.omg.PortableServer.ServantLocatorOperations#postinvoke(byte[],
76      * org.omg.PortableServer.POA, java.lang.String, java.lang.Object, org.omg.PortableServer.Servant)
77      */

78     public void postinvoke(byte[] oid, POA JavaDoc adapter, String JavaDoc operation, Object JavaDoc theCookie, Servant JavaDoc theServant)
79     {
80         try
81         {
82             if (LOG.isDebugEnabled()) LOG.debug("Postinvoke " + operation + " on " + new String JavaDoc(oid));
83             StatelessObjectInfo info = (StatelessObjectInfo) objectInfoTable.get(new String JavaDoc(oid));
84             ObjectPool pool = info.getServantPool();
85             pool.returnObject(theServant);
86         }
87         catch (Exception JavaDoc e)
88         {
89             if (LOG.isErrorEnabled())
90             {
91                 LOG.error(e.getMessage(), e);
92             }
93             CompletionStatus JavaDoc completionStatus = (theCookie == null ? CompletionStatus.COMPLETED_MAYBE :
94                     CompletionStatus.COMPLETED_NO);
95             throw new INTERNAL JavaDoc(e.getMessage(), ERR_MINOR_CODE, completionStatus);
96         }
97     }
98
99 }
100
Popular Tags