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.servicemix.common;
018
019 import java.lang.reflect.Method;
020
021 import javax.jbi.component.ComponentContext;
022
023 public abstract class Container {
024
025 public enum Type {
026 ServiceMix3,
027 ServiceMix4,
028 Unknown
029 }
030
031 protected final ComponentContext context;
032
033 protected Container(ComponentContext context) {
034 this.context = context;
035 }
036
037 public String toString() {
038 return getType().toString();
039 }
040
041 public abstract Type getType();
042
043 public abstract boolean handleTransactions();
044
045 public ClassLoader getSharedLibraryClassLoader(String name) {
046 throw new UnsupportedOperationException("Can not access shared libraries");
047 }
048
049 public ClassLoader getComponentClassLoader(String name) {
050 throw new UnsupportedOperationException("Can not access components");
051 }
052
053 public static Container detect(ComponentContext context) {
054 try {
055 String clName = context.getClass().getName();
056 if ("org.apache.servicemix.jbi.framework.ComponentContextImpl".equals(clName)) {
057 return new Smx3Container(context);
058 }
059 if ("org.apache.servicemix.jbi.runtime.impl.ComponentContextImpl".equals(clName)) {
060 return new Smx4Container(context);
061 }
062 } catch (Throwable t) {
063 }
064 return new UnknownContainer(context);
065 }
066
067 public static class Smx3Container extends Container {
068 private boolean handleTransactions;
069 private Object container;
070 public Smx3Container(ComponentContext context) {
071 super(context);
072 try {
073 Method getContainerMth = context.getClass().getMethod("getContainer");
074 container = getContainerMth.invoke(context, new Object[0]);
075 } catch (Throwable t) {
076 }
077 try {
078 Method isUseNewTransactionModelMth = container.getClass().getMethod("isUseNewTransactionModel");
079 Boolean b = (Boolean) isUseNewTransactionModelMth.invoke(container);
080 handleTransactions = !b;
081 } catch (Throwable t) {
082 handleTransactions = true;
083 }
084 }
085 public Type getType() {
086 return Type.ServiceMix3;
087 }
088 public boolean handleTransactions() {
089 return handleTransactions;
090 }
091 public Object getSmx3Container() {
092 return container;
093 }
094
095 public ClassLoader getSharedLibraryClassLoader(String name) {
096 try {
097 Object registry = container.getClass().getMethod("getRegistry").invoke(container);
098 Object sl = registry.getClass().getMethod("getSharedLibrary", String.class).invoke(registry, name);
099 return (ClassLoader) sl.getClass().getMethod("getClassLoader").invoke(sl);
100 } catch (Throwable t) {
101 return null;
102 }
103 }
104
105 public ClassLoader getComponentClassLoader(String name) {
106 try {
107 Object registry = container.getClass().getMethod("getRegistry").invoke(container);
108 Object mbean = registry.getClass().getMethod("getComponent", String.class).invoke(registry, name);
109 Object cmp = mbean.getClass().getMethod("getComponent").invoke(mbean);
110 return cmp.getClass().getClassLoader();
111 } catch (Throwable t) {
112 return null;
113 }
114 }
115 }
116
117 public static class Smx4Container extends Container {
118 public Smx4Container(ComponentContext context) {
119 super(context);
120 }
121 public Type getType() {
122 return Type.ServiceMix4;
123 }
124 public boolean handleTransactions() {
125 return false;
126 }
127 }
128
129 public static class UnknownContainer extends Container {
130 public UnknownContainer(ComponentContext context) {
131 super(context);
132 }
133 public Type getType() {
134 return Type.Unknown;
135 }
136 public boolean handleTransactions() {
137 return false;
138 }
139 }
140
141 }