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: */package org.apache.geronimo.connector.outbound;
017:
018: import java.io.Externalizable;
019: import java.io.IOException;
020: import java.io.InvalidObjectException;
021: import java.io.ObjectInput;
022: import java.io.ObjectOutput;
023: import java.io.ObjectStreamException;
024: import java.io.Serializable;
025:
026: import javax.resource.spi.ConnectionManager;
027: import javax.security.auth.Subject;
028:
029: import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
030: import org.apache.geronimo.connector.outbound.connectionmanagerconfig.TransactionSupport;
031: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
032: import org.apache.geronimo.gbean.AbstractName;
033: import org.apache.geronimo.gbean.GBeanInfo;
034: import org.apache.geronimo.gbean.GBeanInfoBuilder;
035: import org.apache.geronimo.gbean.GBeanLifecycle;
036: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
037: import org.apache.geronimo.kernel.GBeanNotFoundException;
038: import org.apache.geronimo.kernel.Kernel;
039: import org.apache.geronimo.kernel.KernelRegistry;
040: import org.apache.geronimo.kernel.proxy.ProxyManager;
041: import org.apache.geronimo.security.ContextManager;
042: import org.apache.geronimo.transaction.manager.RecoverableTransactionManager;
043:
044: /**
045: * @version $Revision: 584764 $
046: */
047: public class GenericConnectionManagerGBean extends
048: GenericConnectionManager implements GBeanLifecycle,
049: Serializable, Externalizable {
050: private Kernel kernel;
051: private AbstractName abstractName;
052: //externalizable format version
053: private static final int VERSION = 1;
054:
055: public GenericConnectionManagerGBean() {
056: super ();
057: kernel = null;
058: abstractName = null;
059: }
060:
061: public GenericConnectionManagerGBean(
062: TransactionSupport transactionSupport,
063: PoolingSupport pooling, boolean containerManagedSecurity,
064: ConnectionTracker connectionTracker,
065: RecoverableTransactionManager transactionManager,
066: String objectName, AbstractName abstractName,
067: ClassLoader classLoader, Kernel kernel) {
068: super (transactionSupport, pooling,
069: getSubjectSource(containerManagedSecurity),
070: connectionTracker, transactionManager, objectName,
071: classLoader);
072: this .kernel = kernel;
073: this .abstractName = abstractName;
074: }
075:
076: public ConnectionManager getConnectionManager() {
077: ConnectionManager unproxied = super .getConnectionManager();
078: ProxyManager pm = kernel.getProxyManager();
079: if (pm.isProxy(unproxied)) {
080: return unproxied;
081: } else {
082: return (ConnectionManager) pm.createProxy(kernel
083: .getAbstractNameFor(unproxied), unproxied
084: .getClass().getClassLoader());
085: }
086: }
087:
088: private static SubjectSource getSubjectSource(
089: boolean containerManagedSecurity) {
090: if (containerManagedSecurity) {
091: return new SubjectSource() {
092: public Subject getSubject() {
093: return ContextManager.getNextCaller();
094: }
095: };
096: } else {
097: return null;
098: }
099: }
100:
101: private Object readResolve() throws ObjectStreamException {
102: try {
103: return kernel.getGBean(abstractName);
104: } catch (GBeanNotFoundException e) {
105: throw (ObjectStreamException) new InvalidObjectException(
106: "Could not locate connection manager gbean")
107: .initCause(e);
108: }
109: }
110:
111: public void writeExternal(ObjectOutput out) throws IOException {
112: out.writeInt(VERSION);
113: out.writeObject(kernel.getKernelName());
114: out.writeObject(abstractName);
115: }
116:
117: public void readExternal(ObjectInput in) throws IOException,
118: ClassNotFoundException {
119: int version = in.readInt();
120: if (version != VERSION) {
121: throw new IOException("Wrong version, expected " + VERSION
122: + ", got: " + version);
123: }
124: String kernelName = (String) in.readObject();
125: kernel = KernelRegistry.getKernel(kernelName);
126: if (kernel == null) {
127: kernel = KernelRegistry.getSingleKernel();
128: }
129: if (kernel == null) {
130: throw new IOException("No kernel named: '" + kernelName
131: + "' found");
132: }
133: abstractName = (AbstractName) in.readObject();
134: }
135:
136: public static final GBeanInfo GBEAN_INFO;
137:
138: static {
139: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
140: GenericConnectionManagerGBean.class,
141: AbstractConnectionManagerGBean.GBEAN_INFO);
142:
143: infoBuilder.addAttribute("transactionSupport",
144: TransactionSupport.class, true);
145: infoBuilder.addAttribute("pooling", PoolingSupport.class, true);
146: infoBuilder.addAttribute("containerManagedSecurity",
147: Boolean.TYPE, true);
148:
149: infoBuilder.addAttribute("objectName", String.class, false);
150: infoBuilder.addAttribute("abstractName", AbstractName.class,
151: false);
152: infoBuilder.addAttribute("classLoader", ClassLoader.class,
153: false);
154: infoBuilder.addAttribute("kernel", Kernel.class, false);
155:
156: infoBuilder.addReference("ConnectionTracker",
157: ConnectionTracker.class,
158: NameFactory.JCA_CONNECTION_TRACKER);
159: infoBuilder.addReference("TransactionManager",
160: RecoverableTransactionManager.class,
161: NameFactory.JTA_RESOURCE);
162:
163: infoBuilder
164: .setConstructor(new String[] { "transactionSupport",
165: "pooling", "containerManagedSecurity",
166: "ConnectionTracker", "TransactionManager",
167: "objectName", "abstractName", "classLoader",
168: "kernel" });
169:
170: GBEAN_INFO = infoBuilder.getBeanInfo();
171: }
172:
173: public static GBeanInfo getGBeanInfo() {
174: return GBEAN_INFO;
175: }
176:
177: }
|