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.io.IOException;
020    import java.rmi.RMISecurityManager;
021    
022    import net.jini.core.discovery.LookupLocator;
023    import net.jini.core.entry.Entry;
024    import net.jini.core.lookup.ServiceRegistrar;
025    import net.jini.core.lookup.ServiceTemplate;
026    import net.jini.core.transaction.Transaction;
027    import net.jini.core.transaction.TransactionFactory;
028    import net.jini.core.transaction.server.TransactionManager;
029    
030    /**
031     * @version $Revision: 15488 $
032     */
033    public class TransactionHelper {
034        private static TransactionHelper me;
035    
036        private String uri;
037        private TransactionManager trManager;
038    
039        public TransactionHelper(String uri) {
040            this.uri = uri;
041        }
042    
043        public static TransactionHelper getInstance(String uri) {
044            if (me == null) {
045                me = new TransactionHelper(uri);
046            }
047            return me;
048        }
049    
050        /**
051         * getJiniTransaction Returns a transaction manager proxy.
052         * 
053         * @param timeout - The length of time our transaction should live before timing out.
054         * @return Transaction.Created
055         * @throws Exception can be thrown
056         */
057        public Transaction.Created getJiniTransaction(long timeout) throws Exception {
058            if (null == trManager) {
059                trManager = findTransactionManager(uri);
060            }
061            Transaction.Created tCreated = TransactionFactory.create(trManager, timeout);
062            return tCreated;
063        }
064    
065        private TransactionManager findTransactionManager(String uri) throws IOException, ClassNotFoundException {
066            if (System.getSecurityManager() == null) {
067                System.setSecurityManager(new RMISecurityManager());
068            }
069    
070            // Creating service template to find transaction manager service by matching fields.
071            Class<?>[] classes = new Class<?>[] {net.jini.core.transaction.server.TransactionManager.class};
072            // Name sn = new Name("*");
073            ServiceTemplate tmpl = new ServiceTemplate(null, classes, new Entry[] {});
074    
075            // Creating a lookup locator
076            LookupLocator locator = new LookupLocator(uri);
077            ServiceRegistrar sr = locator.getRegistrar();
078    
079            TransactionManager tm = (TransactionManager) sr.lookup(tmpl);
080            return tm;
081        }
082    
083    }