1 | // SECTION-START[License Header] |
2 | // <editor-fold defaultstate="collapsed" desc=" Generated License "> |
3 | /* |
4 | * Copyright (c) 2010 The JOMC Project |
5 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * |
12 | * o Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * |
15 | * o Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in |
17 | * the documentation and/or other materials provided with the |
18 | * distribution. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
27 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
30 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | * |
32 | * $Id: DefaultLocator.java 1346 2010-01-19 12:16:43Z schulte2005 $ |
33 | * |
34 | */ |
35 | // </editor-fold> |
36 | // SECTION-END |
37 | package org.jomc.ri; |
38 | |
39 | import java.io.IOException; |
40 | import java.net.URI; |
41 | import java.text.MessageFormat; |
42 | import java.util.ResourceBundle; |
43 | import javax.naming.Context; |
44 | import javax.naming.InitialContext; |
45 | import javax.naming.NamingException; |
46 | import javax.rmi.PortableRemoteObject; |
47 | import org.jomc.spi.Locator; |
48 | |
49 | // SECTION-START[Documentation] |
50 | // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> |
51 | /** |
52 | * Default {@code Locator} implementation. |
53 | * |
54 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0 |
55 | * @version $Id: DefaultLocator.java 1346 2010-01-19 12:16:43Z schulte2005 $ |
56 | */ |
57 | // </editor-fold> |
58 | // SECTION-END |
59 | // SECTION-START[Annotations] |
60 | // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> |
61 | @javax.annotation.Generated( value = "org.jomc.tools.JavaSources", |
62 | comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-15/jomc-tools" ) |
63 | // </editor-fold> |
64 | // SECTION-END |
65 | public class DefaultLocator implements Locator |
66 | { |
67 | // SECTION-START[DefaultLocator] |
68 | |
69 | /** Constant for the {@code 'jndi'} URI scheme. */ |
70 | private static final String JNDI_URI_SCHEME = "jndi"; |
71 | |
72 | /** Constant for the {@code 'jndi+rmi'} URI scheme. */ |
73 | private static final String JNDI_RMI_URI_SCHEME = "jndi+rmi"; |
74 | |
75 | /** URI schemes supported by this {@code Locator} implementation. */ |
76 | private static final String[] SUPPORTED_URI_SCHEMES = |
77 | { |
78 | JNDI_URI_SCHEME, JNDI_RMI_URI_SCHEME |
79 | }; |
80 | |
81 | /** The JNDI context of the instance. */ |
82 | private Context jndiContext; |
83 | |
84 | /** |
85 | * Gets a flag indicating support for a given location URI. |
86 | * |
87 | * @param location The location URI to test support for. |
88 | * |
89 | * @return {@code true} if {@code location} is supported by this implementation; {@code false} else. |
90 | * |
91 | * @throws NullPointerException if {@code location} is {@code null}. |
92 | */ |
93 | public boolean isLocationSupported( final URI location ) |
94 | { |
95 | if ( location == null ) |
96 | { |
97 | throw new NullPointerException( "location" ); |
98 | } |
99 | |
100 | final String scheme = location.getScheme(); |
101 | for ( String s : SUPPORTED_URI_SCHEMES ) |
102 | { |
103 | if ( s.equals( scheme ) ) |
104 | { |
105 | return true; |
106 | } |
107 | } |
108 | |
109 | return false; |
110 | } |
111 | |
112 | /** |
113 | * Gets the JNDI context of the instance. |
114 | * |
115 | * @return The JNDI context of the instance. |
116 | * |
117 | * @throws NamingException if getting the context fails. |
118 | */ |
119 | public Context getJndiContext() throws NamingException |
120 | { |
121 | if ( this.jndiContext == null ) |
122 | { |
123 | this.jndiContext = new InitialContext(); |
124 | } |
125 | |
126 | return this.jndiContext; |
127 | } |
128 | |
129 | /** |
130 | * Gets the JNDI name for a given location. |
131 | * |
132 | * @param location The location to get a JNDI name for. |
133 | * |
134 | * @return The JNDI name for {@code location}. |
135 | * |
136 | * @throws NullPointerException if {@code location} is {@code null}. |
137 | */ |
138 | public String getJndiName( final URI location ) |
139 | { |
140 | if ( location == null ) |
141 | { |
142 | throw new NullPointerException( "location" ); |
143 | } |
144 | |
145 | String name = location.getSchemeSpecificPart(); |
146 | if ( name == null || name.replace( '/', ' ' ).trim().length() == 0 ) |
147 | { |
148 | name = ""; |
149 | } |
150 | if ( location.getFragment() != null ) |
151 | { |
152 | name += '#' + location.getFragment(); |
153 | } |
154 | |
155 | return name; |
156 | } |
157 | |
158 | public <T> T getObject( final Class<T> specification, final URI location ) throws IOException |
159 | { |
160 | if ( specification == null ) |
161 | { |
162 | throw new NullPointerException( "specification" ); |
163 | } |
164 | if ( location == null ) |
165 | { |
166 | throw new NullPointerException( "location" ); |
167 | } |
168 | |
169 | try |
170 | { |
171 | T object = null; |
172 | |
173 | final String scheme = location.getScheme(); |
174 | if ( !this.isLocationSupported( location ) ) |
175 | { |
176 | throw new IOException( this.getMessage( "unsupportedUriScheme", new Object[] |
177 | { |
178 | location.getScheme() |
179 | } ) ); |
180 | |
181 | } |
182 | |
183 | final Object jndiObject = this.getJndiContext().lookup( this.getJndiName( location ) ); |
184 | |
185 | if ( JNDI_URI_SCHEME.equals( scheme ) ) |
186 | { |
187 | object = (T) jndiObject; |
188 | } |
189 | else if ( JNDI_RMI_URI_SCHEME.equals( scheme ) ) |
190 | { |
191 | object = (T) PortableRemoteObject.narrow( jndiObject, specification ); |
192 | } |
193 | |
194 | return object; |
195 | } |
196 | catch ( final NamingException e ) |
197 | { |
198 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
199 | } |
200 | } |
201 | |
202 | private String getMessage( final String key, final Object args ) |
203 | { |
204 | return new MessageFormat( ResourceBundle.getBundle( DefaultLocator.class.getName().replace( '.', '/' ) ). |
205 | getString( key ) ).format( args ); |
206 | |
207 | } |
208 | |
209 | // SECTION-END |
210 | // SECTION-START[Constructors] |
211 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> |
212 | |
213 | /** Creates a new {@code DefaultLocator} instance. */ |
214 | @javax.annotation.Generated( value = "org.jomc.tools.JavaSources", |
215 | comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-15/jomc-tools" ) |
216 | public DefaultLocator() |
217 | { |
218 | // SECTION-START[Default Constructor] |
219 | super(); |
220 | // SECTION-END |
221 | } |
222 | // </editor-fold> |
223 | // SECTION-END |
224 | // SECTION-START[Dependencies] |
225 | // SECTION-END |
226 | // SECTION-START[Properties] |
227 | // SECTION-END |
228 | // SECTION-START[Messages] |
229 | // SECTION-END |
230 | } |