001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.javaspace;
018    
019    import java.rmi.RMISecurityManager;
020    
021    import net.jini.core.discovery.LookupLocator;
022    import net.jini.core.entry.Entry;
023    import net.jini.core.lookup.ServiceRegistrar;
024    import net.jini.core.lookup.ServiceTemplate;
025    import net.jini.lookup.entry.Name;
026    import net.jini.space.JavaSpace;
027    
028    /**
029     * @version $Revision: 15488 $
030     */
031    public final class JiniSpaceAccessor {
032    
033        private JiniSpaceAccessor() {
034        }
035        
036        public static JavaSpace findSpace(String url, String spaceName) throws Exception {
037    
038            if (System.getSecurityManager() == null) {
039                System.setSecurityManager(new RMISecurityManager());
040            }
041    
042            Class<?>[] classes = new Class<?>[] {net.jini.space.JavaSpace.class};
043            Name sn = new Name(spaceName);
044            ServiceTemplate tmpl = new ServiceTemplate(null/* serviceID */, classes, new Entry[] {sn});
045    
046            LookupLocator locator = new LookupLocator(url); // <protocol>://<hostname>
047            ServiceRegistrar sr = locator.getRegistrar();
048            JavaSpace space = (JavaSpace) sr.lookup(tmpl);
049    
050            return space;
051        }
052    }