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.connector.outbound;
018    
019    import java.util.Hashtable;
020    
021    import javax.management.ObjectName;
022    
023    import org.apache.geronimo.j2ee.management.impl.InvalidObjectNameException;
024    import org.apache.geronimo.kernel.ObjectNameUtil;
025    import org.apache.geronimo.management.geronimo.JCAConnectionFactory;
026    import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
027    
028    /**
029     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
030     */
031    public class JCAConnectionFactoryImpl implements JCAConnectionFactory  {
032        private final String objectName;
033        private final JCAManagedConnectionFactory managedConnectionFactory;
034    
035        public JCAConnectionFactoryImpl(String objectName, JCAManagedConnectionFactory managedConnectionFactory) {
036            // todo do we really need to do this at runtime - shouldn't the builder set this up correctly?
037            ObjectName myObjectName = ObjectNameUtil.getObjectName(objectName);
038            verifyObjectName(myObjectName);
039    
040            this.objectName = objectName;
041            this.managedConnectionFactory = managedConnectionFactory;
042        }
043    
044        public String getManagedConnectionFactory() {
045            return managedConnectionFactory.getObjectName();
046        }
047    
048        public JCAManagedConnectionFactory getManagedConnectionFactoryInstance() {
049            return managedConnectionFactory;
050        }
051    
052        public String getObjectName() {
053            return objectName;
054        }
055    
056        public boolean isStateManageable() {
057            return false;
058        }
059    
060        public boolean isStatisticsProvider() {
061            return false;
062        }
063    
064        public boolean isEventProvider() {
065            return false;
066        }
067    
068        /**
069         * ObjectName must match this pattern:
070         * <p/>
071         * domain:j2eeType=JCAConnectionFactory,name=MyName,J2EEServer=MyServer,JCAResource=MyJCAResource
072         */
073        private void verifyObjectName(ObjectName objectName) {
074            if (objectName.isPattern()) {
075                throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
076            }
077            Hashtable keyPropertyList = objectName.getKeyPropertyList();
078            if (!"JCAConnectionFactory".equals(keyPropertyList.get("j2eeType"))) {
079                throw new InvalidObjectNameException("JCAConnectionFactory object name j2eeType property must be 'JCAConnectionFactory'", objectName);
080            }
081            if (!keyPropertyList.containsKey("name")) {
082                throw new InvalidObjectNameException("JCAConnectionFactory object must contain a name property", objectName);
083            }
084            if (!keyPropertyList.containsKey("J2EEServer")) {
085                throw new InvalidObjectNameException("JCAConnectionFactory object name must contain a J2EEServer property", objectName);
086            }
087            if (!keyPropertyList.containsKey("JCAResource")) {
088                throw new InvalidObjectNameException("JCAResource object name must contain a JCAResource property", objectName);
089            }
090    //        if (keyPropertyList.size() != 4) {
091    //            throw new InvalidObjectNameException("JCAConnectionFactory object name can only have j2eeType, name, JCAResource, and J2EEServer properties", objectName);
092    //        }
093        }
094    }