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;
018    
019    import java.util.ArrayList;
020    import java.util.Arrays;
021    import java.util.Collection;
022    import java.util.HashSet;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Set;
026    
027    import org.apache.geronimo.j2ee.management.impl.Util;
028    import org.apache.geronimo.management.geronimo.JCAAdminObject;
029    import org.apache.geronimo.management.geronimo.JCAConnectionFactory;
030    import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
031    import org.apache.geronimo.management.geronimo.JCAResource;
032    import org.apache.geronimo.management.geronimo.JCAResourceAdapter;
033    
034    /**
035     * @version $Rev: 550546 $ $Date: 2007-06-25 12:52:11 -0400 (Mon, 25 Jun 2007) $
036     */
037    public class JCAResourceImpl implements JCAResource  {
038        private final String objectName;
039    
040        private final Collection connectionFactories;
041        private final Collection resourceAdapters;
042        private final Collection adminObjects;
043    
044        public JCAResourceImpl(String objectName, Collection connectionFactories, Collection resourceAdapters, Collection adminObjects) {
045            this.objectName = objectName;
046            this.connectionFactories = connectionFactories;
047            this.resourceAdapters = resourceAdapters;
048            this.adminObjects = adminObjects;
049        }
050    
051        public String[] getConnectionFactories() {
052            return Util.getObjectNames(getConnectionFactoryInstances());
053        }
054    
055        public String[] getResourceAdapterInstanceNames() {
056            ArrayList temp = new ArrayList();
057            for (Iterator iterator = resourceAdapters.iterator(); iterator.hasNext();) {
058                JCAResourceAdapter resourceAdapter = (JCAResourceAdapter) iterator.next();
059                temp.add(resourceAdapter.getObjectName());
060            }
061            return (String[])temp.toArray(new String[temp.size()]);
062        }
063    
064        public JCAResourceAdapter[] getResourceAdapterInstances() {
065            return (JCAResourceAdapter[])resourceAdapters.toArray(new JCAResourceAdapter[resourceAdapters.size()]);
066        }
067    
068        public JCAConnectionFactory[] getConnectionFactoryInstances() {
069            return (JCAConnectionFactory[])connectionFactories.toArray(new JCAConnectionFactory[connectionFactories.size()]);
070        }
071    
072        public JCAManagedConnectionFactory[] getOutboundFactories() {
073            return getOutboundFactories((String[]) null);
074        }
075    
076        public JCAManagedConnectionFactory[] getOutboundFactories(String connectionFactoryInterface) {
077            return getOutboundFactories(new String[]{connectionFactoryInterface});
078        }
079    
080        public JCAManagedConnectionFactory[] getOutboundFactories(String[] connectionFactoryInterfaces) {
081            Set interfaceFilter = null;
082            if (connectionFactoryInterfaces != null) {
083                interfaceFilter = new HashSet(Arrays.asList(connectionFactoryInterfaces));
084            }
085    
086            List list = new ArrayList();
087            for (Iterator iterator = connectionFactories.iterator(); iterator.hasNext();) {
088                JCAConnectionFactory jcaConnectionFactory = (JCAConnectionFactory) iterator.next();
089                JCAManagedConnectionFactory mcf = jcaConnectionFactory.getManagedConnectionFactoryInstance();
090                if (interfaceFilter == null || interfaceFilter.contains(mcf.getConnectionFactoryInterface())) {
091                    list.add(mcf);
092                    continue;
093                }
094                for (int m = 0; m < mcf.getImplementedInterfaces().length; m++) {
095                    String iface = mcf.getImplementedInterfaces()[m];
096                    if (interfaceFilter == null || interfaceFilter.contains(iface)) {
097                        list.add(mcf);
098                        break;
099                    }
100                }
101            }
102            return (JCAManagedConnectionFactory[]) list.toArray(new JCAManagedConnectionFactory[list.size()]);
103        }
104    
105        public String[] getAdminObjects() {
106            return Util.getObjectNames(getAdminObjectInstances());
107        }
108    
109        public JCAAdminObject[] getAdminObjectInstances() {
110            return (JCAAdminObject[]) adminObjects.toArray(new JCAAdminObject[adminObjects.size()]);
111        }
112    
113        public JCAAdminObject[] getAdminObjectInstances(String adminObjectInterface) {
114            return getAdminObjectInstances(new String[] {adminObjectInterface});
115        }
116    
117        public JCAAdminObject[] getAdminObjectInstances(String[] adminObjectInterfaces) {
118            Set interfaceFilter = null;
119            if (adminObjectInterfaces != null) {
120                interfaceFilter = new HashSet(Arrays.asList(adminObjectInterfaces));
121            }
122    
123            List list = new ArrayList();
124    
125            for (Iterator iterator = adminObjects.iterator(); iterator.hasNext();) {
126                JCAAdminObject adminObject = (JCAAdminObject) iterator.next();
127                if (interfaceFilter == null || interfaceFilter.contains(adminObject.getAdminObjectInterface())) {
128                    list.add(adminObject);
129                }
130            }
131    
132            return (JCAAdminObject[]) list.toArray(new JCAAdminObject[list.size()]);
133        }
134    
135    
136        public String getObjectName() {
137            return objectName;
138        }
139    
140        public boolean isStateManageable() {
141            return false;
142        }
143    
144        public boolean isStatisticsProvider() {
145            return false;
146        }
147    
148        public boolean isEventProvider() {
149            return false;
150        }
151    }