001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.pacl.jndi;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.util.Hashtable;
020    
021    import javax.naming.Binding;
022    import javax.naming.Context;
023    import javax.naming.Name;
024    import javax.naming.NameClassPair;
025    import javax.naming.NameParser;
026    import javax.naming.NamingEnumeration;
027    import javax.naming.NamingException;
028    import javax.naming.spi.NamingManager;
029    
030    /**
031     * @author Raymond Augé
032     */
033    public class SchemeAwareContextWrapper implements Context {
034    
035            public SchemeAwareContextWrapper(Context context) {
036                    _context = context;
037            }
038    
039            public Object addToEnvironment(String propName, Object propVal)
040                    throws NamingException {
041    
042                    return _context.addToEnvironment(propName, propVal);
043            }
044    
045            public void bind(Name name, Object obj) throws NamingException {
046                    Context context = getContext(name);
047    
048                    context.bind(name, obj);
049            }
050    
051            public void bind(String name, Object obj) throws NamingException {
052                    Context context = getContext(name);
053    
054                    context.bind(name, obj);
055            }
056    
057            public void close() throws NamingException {
058                    _context.close();
059            }
060    
061            public Name composeName(Name name, Name prefix) throws NamingException {
062                    return _context.composeName(name, prefix);
063            }
064    
065            public String composeName(String name, String prefix)
066                    throws NamingException {
067    
068                    return _context.composeName(name, prefix);
069            }
070    
071            public Context createSubcontext(Name name) throws NamingException {
072                    Context context = getContext(name);
073    
074                    return context.createSubcontext(name);
075            }
076    
077            public Context createSubcontext(String name) throws NamingException {
078                    Context context = getContext(name);
079    
080                    return context.createSubcontext(name);
081            }
082    
083            public void destroySubcontext(Name name) throws NamingException {
084                    Context context = getContext(name);
085    
086                    context.destroySubcontext(name);
087            }
088    
089            public void destroySubcontext(String name) throws NamingException {
090                    Context context = getContext(name);
091    
092                    context.destroySubcontext(name);
093            }
094    
095            @Override
096            public boolean equals(Object obj) {
097                    if (this == obj) {
098                            return true;
099                    }
100    
101                    if (!(obj instanceof Context)) {
102                            return false;
103                    }
104    
105                    Context context = (Context)obj;
106    
107                    if (Validator.equals(_context, context)) {
108                            return true;
109                    }
110    
111                    return false;
112            }
113    
114            public Hashtable<?, ?> getEnvironment() throws NamingException {
115                    return _context.getEnvironment();
116            }
117    
118            public String getNameInNamespace() throws NamingException {
119                    return _context.getNameInNamespace();
120            }
121    
122            public NameParser getNameParser(Name name) throws NamingException {
123                    Context context = getContext(name);
124    
125                    return context.getNameParser(name);
126            }
127    
128            public NameParser getNameParser(String name) throws NamingException {
129                    Context context = getContext(name);
130    
131                    return context.getNameParser(name);
132            }
133    
134            @Override
135            public int hashCode() {
136                    return _context.hashCode();
137            }
138    
139            public NamingEnumeration<NameClassPair> list(Name name)
140                    throws NamingException {
141    
142                    Context context = getContext(name);
143    
144                    return context.list(name);
145            }
146    
147            public NamingEnumeration<NameClassPair> list(String name)
148                    throws NamingException {
149    
150                    Context context = getContext(name);
151    
152                    return context.list(name);
153            }
154    
155            public NamingEnumeration<Binding> listBindings(Name name)
156                    throws NamingException {
157    
158                    Context context = getContext(name);
159    
160                    return context.listBindings(name);
161            }
162    
163            public NamingEnumeration<Binding> listBindings(String name)
164                    throws NamingException {
165    
166                    Context context = getContext(name);
167    
168                    return context.listBindings(name);
169            }
170    
171            public Object lookup(Name name) throws NamingException {
172                    Context context = getContext(name);
173    
174                    return context.lookup(name);
175            }
176    
177            public Object lookup(String name) throws NamingException {
178                    Context context = getContext(name);
179    
180                    return context.lookup(name);
181            }
182    
183            public Object lookupLink(Name name) throws NamingException {
184                    Context context = getContext(name);
185    
186                    return context.lookupLink(name);
187            }
188    
189            public Object lookupLink(String name) throws NamingException {
190                    Context context = getContext(name);
191    
192                    return context.lookupLink(name);
193            }
194    
195            public void rebind(Name name, Object obj) throws NamingException {
196                    Context context = getContext(name);
197    
198                    context.rebind(name, obj);
199            }
200    
201            public void rebind(String name, Object obj) throws NamingException {
202                    Context context = getContext(name);
203    
204                    context.rebind(name, obj);
205            }
206    
207            public Object removeFromEnvironment(String propName)
208                    throws NamingException {
209    
210                    return _context.removeFromEnvironment(propName);
211            }
212    
213            public void rename(Name oldName, Name newName) throws NamingException {
214                    Context context = getContext(oldName);
215    
216                    context.rename(oldName, newName);
217            }
218    
219            public void rename(String oldName, String newName) throws NamingException {
220                    Context context = getContext(oldName);
221    
222                    context.rename(oldName, newName);
223            }
224    
225            public void unbind(Name name) throws NamingException {
226                    Context context = getContext(name);
227    
228                    context.unbind(name);
229            }
230    
231            public void unbind(String name) throws NamingException {
232                    Context context = getContext(name);
233    
234                    context.unbind(name);
235            }
236    
237            protected Context getContext(Name name) throws NamingException {
238                    return getContext(name.toString());
239            }
240    
241            protected Context getContext(String name) throws NamingException {
242                    String scheme = null;
243    
244                    int x = name.indexOf(':');
245                    int y = name.indexOf('/');
246    
247                    if ((x > 0) && ((y == -1) || (x < y))) {
248                            scheme = name.substring(0, x);
249                    }
250    
251                    if (scheme != null) {
252                            Context context = NamingManager.getURLContext(
253                                    scheme, _context.getEnvironment());
254    
255                            if (context != null) {
256                                    return context;
257                            }
258                    }
259    
260                    return _context;
261            }
262    
263            private Context _context;
264    
265    }