KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > iiop > CodebaseIORInterceptor


1 /*
2  * <Add library description here>
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * CodebaseIORInterceptor.java
20  *
21  * This interceptor is responsible for setting the code base within an IOR so
22  * that the stub code can be download when required.
23  *
24  * $Revision: 1.1 $
25  */

26
27 // package path
28
package com.rift.coad.lib.interceptor.iiop;
29
30 // java imports
31
import java.net.InetAddress JavaDoc;
32 import org.omg.CORBA.Any JavaDoc;
33 import org.omg.CORBA.LocalObject JavaDoc;
34 import org.omg.CORBA.ORB JavaDoc;
35 import org.omg.IOP.Codec JavaDoc;
36 import org.omg.IOP.TaggedComponent JavaDoc;
37 import org.omg.IOP.TAG_JAVA_CODEBASE JavaDoc;
38 import org.omg.PortableInterceptor.IORInterceptor JavaDoc;
39 import org.omg.PortableInterceptor.IORInfo JavaDoc;
40
41 // logging import
42
import org.apache.log4j.Logger;
43
44 // coadunation imports
45
import com.rift.coad.lib.configuration.Configuration;
46 import com.rift.coad.lib.configuration.ConfigurationFactory;
47 import com.rift.coad.lib.deployment.DeploymentLoader;
48 import com.rift.coad.lib.interceptor.InterceptorException;
49 import com.rift.coad.lib.httpd.RequestListenerThread;
50 import com.rift.coad.lib.httpd.HttpDaemon;
51
52 /**
53  * This interceptor is responsible for setting the code base within an IOR so
54  * that the stub code can be download when required.
55  *
56  * @author Brett Chaldecott
57  */

58 public class CodebaseIORInterceptor extends LocalObject JavaDoc implements
59         IORInterceptor JavaDoc {
60     
61     // class constants
62
private final static String JavaDoc URL_FORMAT = "http://%s:%d/codebase/%s";
63     
64     // the class log variable
65
protected static Logger log =
66             Logger.getLogger(CodebaseIORInterceptor.class);
67     
68     // private member variables
69
private String JavaDoc hostname = null;
70     private int port = 0;
71     private Codec JavaDoc codec = null;
72     
73     /**
74      * Creates a new instance of CodebaseIORInterceptor.
75      *
76      * @exception InterceptorException
77      */

78     public CodebaseIORInterceptor(Codec JavaDoc codec) throws InterceptorException {
79         this.codec = codec;
80         try {
81             Configuration config = ConfigurationFactory.getInstance().getConfig(
82                     RequestListenerThread.class);
83             // set the port
84
hostname = config.getString(RequestListenerThread.HTTP_HOST,
85                     InetAddress.getLocalHost().getCanonicalHostName());
86             port = (int)config.getLong(RequestListenerThread.HTTP_PORT,
87                     HttpDaemon.DEFAULT_PORT);
88             
89         } catch (Exception JavaDoc ex) {
90             throw new InterceptorException("Failed to init the code base " +
91                     "IOR interceptor because : " + ex.getMessage(),ex);
92         }
93     }
94     
95     
96     /**
97      * This method returns the name of this interceptor
98      *
99      * @return the name of this interceptor.
100      */

101     public String JavaDoc name() {
102         return "CodebaseIORInterceptor";
103     }
104     
105     
106     /**
107      * This method is called to destroy this interceptor
108      */

109     public void destroy() {
110         // do nothing
111
}
112     
113     
114     /**
115      * A server side ORB calls the establish_components operation on all
116      * registered IORInterceptor instances when it is assembling the list of
117      * components that will be included in the profile or profiles of an object
118      * reference.
119      *
120      * @param info The information required to setup this ior.
121      */

122     public void establish_components(IORInfo JavaDoc info) {
123         try {
124             String JavaDoc stubcodeName = DeploymentLoader.ClassLoaderLookup.getInstance().
125                     getStubCodeForLoader(Thread.currentThread().
126                     getContextClassLoader());
127             if (stubcodeName != null) {
128                 String JavaDoc url = String.format(URL_FORMAT,hostname,port,
129                         stubcodeName);
130                 Any JavaDoc any = ORB.init().create_any();
131                 any.insert_string(url);
132                 info.add_ior_component(new TaggedComponent JavaDoc(
133                         TAG_JAVA_CODEBASE.value,codec.encode_value(any)));
134             }
135         } catch (Exception JavaDoc ex) {
136             log.error("Failed to add the entry to the ior : " + ex.getMessage(),
137                     ex);
138         }
139     }
140 }
141
Popular Tags