KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > calculator > ComplexCalculatorImpl


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

5 package calculator;
6
7 import java.net.URL JavaDoc;
8 import java.net.URLClassLoader JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.omg.CORBA.INTERNAL JavaDoc;
13 import org.omg.CosNaming.NamingContextExt JavaDoc;
14 import org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc;
15 import org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc;
16 import org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc;
17
18 import ve.luz.ica.jackass.component.ApplicationContext;
19 import ve.luz.ica.jackass.component.StatelessContext;
20 import ve.luz.ica.jackass.component.StatelessHook;
21
22
23 /**
24  * Servant class of calculator::ComplexCalculator CORBA Interface.
25  * Is a example servant of a Stateless Component of Jackass.
26  * This class, been a CORBA Servant of calculator::ComplexCalculator,
27  * should implement {@link StatelessHook} in order to be a
28  * Stateless Component of Jackass.
29  * Note that this implementation depend on a calculator::ICalculator
30  * (implemented by another Jackass Stateless Component).
31  * @author Carlos Arévalo, Nelson Arapé
32  */

33 public class ComplexCalculatorImpl extends ComplexCalculatorPOA implements StatelessHook
34 {
35     private static final Log LOG = LogFactory.getLog(ComplexCalculatorImpl.class);
36
37     private StatelessContext compContext = null;
38     private ApplicationContext appContext = null;
39
40     /**
41      * Adds op1 to op2 and returns the result.
42      * @param op1 the first operand
43      * @param op2 the second operand
44      * @return the {@link Complex} result of adding the two operands
45      * @see calculator.ComplexCalculatorOperations#add(calculator.Complex, calculator.Complex)
46      */

47     public Complex add(Complex op1, Complex op2)
48     {
49         if (LOG.isDebugEnabled()) LOG.debug("Parameters:" + op1 + ", " + op2);
50
51         try
52         {
53             URLClassLoader JavaDoc loader = (URLClassLoader JavaDoc) this.getClass().getClassLoader();
54             URL JavaDoc[] urls = loader.getURLs();
55             for (int i = 0; i<urls.length; ++i)
56             {
57                 LOG.info(urls[i].getPath());
58             }
59
60
61             NamingContextExt JavaDoc rootContext = compContext.getRootNamingContext();
62             String JavaDoc calcContext = compContext.getProperty("real_calculator_context");
63
64             org.omg.CORBA.Object JavaDoc obj = rootContext.resolve_str(calcContext);
65             RealCalculator calc = RealCalculatorHelper.narrow(obj);
66             if (LOG.isDebugEnabled()) LOG.debug("Resolved RealCalculator component:" + calc);
67
68             double real = calc.add(op1.r, op2.r);
69             double imag = calc.add(op1.i, op2.i);
70             Complex c = new Complex(real, imag);
71
72             if (LOG.isDebugEnabled()) LOG.debug("Result:" + c.r + " " + c.i + "i");
73             return c;
74         }
75         catch (NotFound JavaDoc e)
76         {
77             String JavaDoc errMsg = "RealCalculator component not found.";
78
79             if (LOG.isErrorEnabled())
80             {
81                 errMsg += "Why:" + e.why + ". Rest of the name:" + e.rest_of_name;
82                 LOG.error(errMsg, e);
83             }
84
85             throw new INTERNAL JavaDoc(errMsg);
86         }
87         catch (CannotProceed JavaDoc e)
88         {
89             String JavaDoc errMsg = "System gave up when it was looking for a RealCalculator component.";
90
91             if (LOG.isErrorEnabled())
92             {
93                 errMsg += "Rest of the name:" + e.rest_of_name;
94                 LOG.error(errMsg, e);
95             }
96
97             throw new INTERNAL JavaDoc(errMsg);
98         }
99         catch (InvalidName JavaDoc e)
100         {
101             String JavaDoc errMsg = "Cannot find RealCalculator, Invalid name";
102             if (LOG.isErrorEnabled())
103             {
104                 LOG.error(errMsg, e);
105             }
106
107             throw new INTERNAL JavaDoc(errMsg);
108         }
109     }
110
111     /**
112      * @see ve.luz.ica.jackass.component.StatelessHook#jackassSetContext(ve.luz.ica.jackass.component.JackassContext)
113      */

114     public void jackassSetContexts(ApplicationContext appCtx, StatelessContext compCtx)
115     {
116         this.appContext = appCtx;
117         this.compContext = compCtx;
118     }
119
120     /**
121      * @see ve.luz.ica.jackass.component.StatelessHook#jackassCreate()
122      */

123     public void jackassCreate()
124     {
125         // Nothing to do in this sample component
126
}
127
128     /**
129      * @see ve.luz.ica.jackass.component.StatelessHook#jackassRemove()
130      */

131     public void jackassRemove()
132     {
133         // Nothing to do in this sample component
134
}
135 }
136
Popular Tags