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.geronimo.corba.proxy;
018    
019    import java.net.URI;
020    import javax.naming.NameNotFoundException;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.geronimo.gbean.AbstractName;
025    import org.apache.geronimo.gbean.AbstractNameQuery;
026    import org.apache.geronimo.kernel.GBeanNotFoundException;
027    import org.apache.geronimo.kernel.Kernel;
028    import org.apache.geronimo.kernel.repository.Artifact;
029    import org.apache.geronimo.naming.reference.ConfigurationAwareReference;
030    
031    
032    /**
033     * @version $Revision: 451417 $ $Date: 2006-09-29 13:13:22 -0700 (Fri, 29 Sep 2006) $
034     */
035    public final class CORBAProxyReference extends ConfigurationAwareReference {
036    
037        private final static Log log = LogFactory.getLog(CORBAProxyReference.class);
038    
039        private final URI nsCorbaloc;
040        private final String objectName;
041        private final String home;
042    
043        public CORBAProxyReference(Artifact[] configId, AbstractNameQuery abstractNameQuery, URI nsCorbaloc, String objectName, String home) {
044            super(configId, abstractNameQuery);
045            this.nsCorbaloc = nsCorbaloc;
046            this.objectName = objectName;
047            this.home = home;
048            if (log.isDebugEnabled()) {
049                log.debug("<init> " + nsCorbaloc.toString() + ", " + objectName + ", " + abstractNameQuery + ", " + home);
050            }
051        }
052    
053        public String getClassName() {
054            return home;
055        }
056    
057        public Object getContent() throws NameNotFoundException {
058    
059            if (log.isDebugEnabled()) {
060                log.debug("Obtaining home from " + nsCorbaloc.toString() + ", " + objectName + ", " + abstractNameQueries + ", " + home);
061            }
062            AbstractName containerName;
063            try {
064                containerName = resolveTargetName();
065            } catch (GBeanNotFoundException e) {
066                throw (NameNotFoundException) new NameNotFoundException("Could not resolve gbean from name query: " + abstractNameQueries).initCause(e);
067            }
068            Kernel kernel = getKernel();
069            Object proxy;
070            try {
071                //TODO configid objectname might well be wrong kind of thing.
072                proxy = kernel.invoke(containerName, "getHome", new Object[]{nsCorbaloc, objectName}, new String[]{URI.class.getName(), String.class.getName()});
073            } catch (Exception e) {
074                log.error("Could not get proxy from " + containerName, e);
075                throw (IllegalStateException) new IllegalStateException("Could not get proxy").initCause(e);
076            }
077            if (proxy == null) {
078                log.error("Proxy not returned from " + containerName);
079                throw new IllegalStateException("Proxy not returned. Target " + containerName + " not started");
080            }
081            if (!org.omg.CORBA.Object.class.isAssignableFrom(proxy.getClass())) {
082                log.error("Proxy not an instance of expected class org.omg.CORBA.Object from " + containerName);
083                throw new ClassCastException("Proxy not an instance of expected class org.omg.CORBA.Object");
084            }
085            return proxy;
086        }
087    }