FELIX-4582 Fixed formating issues.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1641394 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/BundleWiringImpl.java b/framework/src/main/java/org/apache/felix/framework/BundleWiringImpl.java
index 2028c30..8106b74 100644
--- a/framework/src/main/java/org/apache/felix/framework/BundleWiringImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/BundleWiringImpl.java
@@ -2075,29 +2075,30 @@
// as a race condition, doing any necessary clean up in
// the error handling.
Felix felix = ((BundleImpl) m_wiring.m_revision.getBundle()).getFramework();
+
Set<ServiceReference<WeavingHook>> hooks =
felix.getHooks(WeavingHook.class);
Set<ServiceReference<WovenClassListener>> wovenClassListeners =
- felix.getHooks(WovenClassListener.class);
+ felix.getHooks(WovenClassListener.class);
WovenClassImpl wci = null;
- if (!hooks.isEmpty())
- {
- // Create woven class to be used for hooks.
- wci = new WovenClassImpl(name, m_wiring, bytes);
- try
- {
- transformClass(felix, wci, hooks, wovenClassListeners,
- name, bytes);
- }
- catch (Error e)
- {
- wci.setState(WovenClass.TRANSFORMING_FAILED);
- callWovenClassListeners(felix, wovenClassListeners, wci);
- throw e;
- }
- }
+ if (!hooks.isEmpty())
+ {
+ // Create woven class to be used for hooks.
+ wci = new WovenClassImpl(name, m_wiring, bytes);
+ try
+ {
+ transformClass(felix, wci, hooks, wovenClassListeners,
+ name, bytes);
+ }
+ catch (Error e)
+ {
+ wci.setState(WovenClass.TRANSFORMING_FAILED);
+ callWovenClassListeners(felix, wovenClassListeners, wci);
+ throw e;
+ }
+ }
// Before we actually attempt to define the class, grab
// the lock for this class loader and make sure than no
// other thread has defined this class in the meantime.
@@ -2128,17 +2129,17 @@
try
{
- clazz = defineClass(felix, wovenClassListeners, wci, name,
- clazz, bytes, content, pkgName, lock);
- }
+ clazz = defineClass(felix, wovenClassListeners, wci, name,
+ clazz, bytes, content, pkgName, lock);
+ }
catch (ClassFormatError e)
{
- if(wci != null)
- {
- wci.setState(WovenClass.DEFINE_FAILED);
- callWovenClassListeners(felix, wovenClassListeners, wci);
- }
- }
+ if(wci != null)
+ {
+ wci.setState(WovenClass.DEFINE_FAILED);
+ callWovenClassListeners(felix, wovenClassListeners, wci);
+ }
+ }
// Perform deferred activation without holding the class loader lock,
// if the class we are returning is the instigating class.
@@ -2174,291 +2175,290 @@
return clazz;
}
- protected Class defineClass(Felix felix,
- Set<ServiceReference<WovenClassListener>> wovenClassListeners,
- WovenClassImpl wci, String name, Class clazz, byte[] bytes,
- Content content, String pkgName, Object lock)
- throws ClassFormatError
- {
+ Class defineClass(Felix felix,
+ Set<ServiceReference<WovenClassListener>> wovenClassListeners,
+ WovenClassImpl wci, String name, Class clazz, byte[] bytes,
+ Content content, String pkgName, Object lock)
+ throws ClassFormatError
+ {
- try
- {
- if (clazz == null)
- {
- // If we have a woven class then get the class bytes from
- // it since they may have changed.
- // NOTE: We are taking a snapshot of these values and
- // are not preventing a malbehaving weaving hook from
- // modifying them after the fact. The price of preventing
- // this isn't worth it, since they can already wreck
- // havoc via weaving anyway. However, we do pass the
- // snapshot values into the woven class when we mark it
- // as complete so that it will refect the actual values
- // we used to define the class.
- if (wci != null)
- {
- bytes = wci._getBytes();
- List<String> wovenImports = wci.getDynamicImportsInternal();
+ try
+ {
+ if (clazz == null)
+ {
+ // If we have a woven class then get the class bytes from
+ // it since they may have changed.
+ // NOTE: We are taking a snapshot of these values and
+ // are not preventing a malbehaving weaving hook from
+ // modifying them after the fact. The price of preventing
+ // this isn't worth it, since they can already wreck
+ // havoc via weaving anyway. However, we do pass the
+ // snapshot values into the woven class when we mark it
+ // as complete so that it will refect the actual values
+ // we used to define the class.
+ if (wci != null)
+ {
+ bytes = wci._getBytes();
+ List<String> wovenImports = wci.getDynamicImportsInternal();
- // Try to add any woven dynamic imports, since they
- // could potentially be needed when defining the class.
- List<BundleRequirement> allWovenReqs =
- new ArrayList<BundleRequirement>();
- for (String s : wovenImports)
- {
- try
- {
- List<BundleRequirement> wovenReqs =
- ManifestParser.parseDynamicImportHeader(
- m_logger, m_wiring.m_revision, s);
- allWovenReqs.addAll(wovenReqs);
- }
- catch (BundleException ex)
- {
- // There should be no exception here
- // since we checked syntax before adding
- // dynamic import strings to list.
- }
- }
- // Add the dynamic requirements.
- if (!allWovenReqs.isEmpty())
- {
- // Check for duplicate woven imports.
- // First grab existing woven imports, if any.
- Set<String> filters = new HashSet<String>();
- if (m_wiring.m_wovenReqs != null)
- {
- for (BundleRequirement req : m_wiring.m_wovenReqs)
- {
- filters.add(
- ((BundleRequirementImpl) req)
- .getFilter().toString());
- }
- }
- // Then check new woven imports for duplicates
- // against existing and self.
- int idx = allWovenReqs.size();
- while (idx < allWovenReqs.size())
- {
- BundleRequirement wovenReq = allWovenReqs.get(idx);
- String filter = ((BundleRequirementImpl)
- wovenReq).getFilter().toString();
- if (!filters.contains(filter))
- {
- filters.add(filter);
- idx++;
- }
- else
- {
- allWovenReqs.remove(idx);
- }
- }
- // Merge existing with new imports, if any.
- if (!allWovenReqs.isEmpty())
- {
- if (m_wiring.m_wovenReqs != null)
- {
- allWovenReqs.addAll(0, m_wiring.m_wovenReqs);
- }
- m_wiring.m_wovenReqs = allWovenReqs;
- }
- }
- }
+ // Try to add any woven dynamic imports, since they
+ // could potentially be needed when defining the class.
+ List<BundleRequirement> allWovenReqs =
+ new ArrayList<BundleRequirement>();
+ for (String s : wovenImports)
+ {
+ try
+ {
+ List<BundleRequirement> wovenReqs =
+ ManifestParser.parseDynamicImportHeader(
+ m_logger, m_wiring.m_revision, s);
+ allWovenReqs.addAll(wovenReqs);
+ }
+ catch (BundleException ex)
+ {
+ // There should be no exception here
+ // since we checked syntax before adding
+ // dynamic import strings to list.
+ }
+ }
+ // Add the dynamic requirements.
+ if (!allWovenReqs.isEmpty())
+ {
+ // Check for duplicate woven imports.
+ // First grab existing woven imports, if any.
+ Set<String> filters = new HashSet<String>();
+ if (m_wiring.m_wovenReqs != null)
+ {
+ for (BundleRequirement req : m_wiring.m_wovenReqs)
+ {
+ filters.add(
+ ((BundleRequirementImpl) req)
+ .getFilter().toString());
+ }
+ }
+ // Then check new woven imports for duplicates
+ // against existing and self.
+ int idx = allWovenReqs.size();
+ while (idx < allWovenReqs.size())
+ {
+ BundleRequirement wovenReq = allWovenReqs.get(idx);
+ String filter = ((BundleRequirementImpl)
+ wovenReq).getFilter().toString();
+ if (!filters.contains(filter))
+ {
+ filters.add(filter);
+ idx++;
+ }
+ else
+ {
+ allWovenReqs.remove(idx);
+ }
+ }
+ // Merge existing with new imports, if any.
+ if (!allWovenReqs.isEmpty())
+ {
+ if (m_wiring.m_wovenReqs != null)
+ {
+ allWovenReqs.addAll(0, m_wiring.m_wovenReqs);
+ }
+ m_wiring.m_wovenReqs = allWovenReqs;
+ }
+ }
+ }
- int activationPolicy =
- ((BundleImpl) getBundle()).isDeclaredActivationPolicyUsed()
- ? ((BundleRevisionImpl) getBundle()
- .adapt(BundleRevision.class)).getDeclaredActivationPolicy()
- : EAGER_ACTIVATION;
+ int activationPolicy =
+ ((BundleImpl) getBundle()).isDeclaredActivationPolicyUsed()
+ ? ((BundleRevisionImpl) getBundle()
+ .adapt(BundleRevision.class)).getDeclaredActivationPolicy()
+ : EAGER_ACTIVATION;
- // If the revision is using deferred activation, then if
- // we load this class from this revision we need to activate
- // the bundle before returning the class. We will short
- // circuit the trigger matching if the trigger is already
- // tripped.
- boolean isTriggerClass = m_isActivationTriggered
- ? false : m_wiring.m_revision.isActivationTrigger(pkgName);
- if (!m_isActivationTriggered
- && isTriggerClass
- && (activationPolicy == BundleRevisionImpl.LAZY_ACTIVATION)
- && (getBundle().getState() == Bundle.STARTING))
- {
- List deferredList = (List) m_deferredActivation.get();
- if (deferredList == null)
- {
- deferredList = new ArrayList();
- m_deferredActivation.set(deferredList);
- }
- deferredList.add(new Object[] { name, getBundle() });
- }
- // We need to try to define a Package object for the class
- // before we call defineClass() if we haven't already
- // created it.
- if (pkgName.length() > 0)
- {
- if (getPackage(pkgName) == null)
- {
- Object[] params = definePackage(pkgName);
+ // If the revision is using deferred activation, then if
+ // we load this class from this revision we need to activate
+ // the bundle before returning the class. We will short
+ // circuit the trigger matching if the trigger is already
+ // tripped.
+ boolean isTriggerClass = m_isActivationTriggered
+ ? false : m_wiring.m_revision.isActivationTrigger(pkgName);
+ if (!m_isActivationTriggered
+ && isTriggerClass
+ && (activationPolicy == BundleRevisionImpl.LAZY_ACTIVATION)
+ && (getBundle().getState() == Bundle.STARTING))
+ {
+ List deferredList = (List) m_deferredActivation.get();
+ if (deferredList == null)
+ {
+ deferredList = new ArrayList();
+ m_deferredActivation.set(deferredList);
+ }
+ deferredList.add(new Object[] { name, getBundle() });
+ }
+ // We need to try to define a Package object for the class
+ // before we call defineClass() if we haven't already
+ // created it.
+ if (pkgName.length() > 0)
+ {
+ if (getPackage(pkgName) == null)
+ {
+ Object[] params = definePackage(pkgName);
- // This is a harmless check-then-act situation,
- // where threads might be racing to create different
- // classes in the same package, so catch and ignore
- // any IAEs that may occur.
- try
- {
- definePackage(
- pkgName,
- (String) params[0],
- (String) params[1],
- (String) params[2],
- (String) params[3],
- (String) params[4],
- (String) params[5],
- null);
- }
- catch (IllegalArgumentException ex)
- {
- // Ignore.
- }
- }
- }
+ // This is a harmless check-then-act situation,
+ // where threads might be racing to create different
+ // classes in the same package, so catch and ignore
+ // any IAEs that may occur.
+ try
+ {
+ definePackage(
+ pkgName,
+ (String) params[0],
+ (String) params[1],
+ (String) params[2],
+ (String) params[3],
+ (String) params[4],
+ (String) params[5],
+ null);
+ }
+ catch (IllegalArgumentException ex)
+ {
+ // Ignore.
+ }
+ }
+ }
- // If we can load the class from a dex file do so
- if (content instanceof JarContent)
- {
- try
- {
- clazz = getDexFileClass((JarContent) content, name, this);
- }
- catch (Exception ex)
- {
- // Looks like we can't
- }
- }
+ // If we can load the class from a dex file do so
+ if (content instanceof JarContent)
+ {
+ try
+ {
+ clazz = getDexFileClass((JarContent) content, name, this);
+ }
+ catch (Exception ex)
+ {
+ // Looks like we can't
+ }
+ }
- if (clazz == null)
- {
- // If we have a security context, then use it to
- // define the class with it for security purposes,
- // otherwise define the class without a protection domain.
- if (m_wiring.m_revision.getProtectionDomain() != null)
- {
- clazz = defineClass(name, bytes, 0, bytes.length,
- m_wiring.m_revision.getProtectionDomain());
- }
- else
- {
- clazz = defineClass(name, bytes, 0, bytes.length);
- }
- if(wci != null)
- {
- wci.setState(WovenClass.DEFINED);
- callWovenClassListeners(felix, wovenClassListeners, wci);
- }
-
- }
+ if (clazz == null)
+ {
+ // If we have a security context, then use it to
+ // define the class with it for security purposes,
+ // otherwise define the class without a protection domain.
+ if (m_wiring.m_revision.getProtectionDomain() != null)
+ {
+ clazz = defineClass(name, bytes, 0, bytes.length,
+ m_wiring.m_revision.getProtectionDomain());
+ }
+ else
+ {
+ clazz = defineClass(name, bytes, 0, bytes.length);
+ }
+ if(wci != null)
+ {
+ wci.setState(WovenClass.DEFINED);
+ callWovenClassListeners(felix, wovenClassListeners, wci);
+ }
+ }
- // At this point if we have a trigger class, then the deferred
- // activation trigger has tripped.
- if (!m_isActivationTriggered && isTriggerClass && (clazz != null))
- {
- m_isActivationTriggered = true;
- }
- }
- }
- finally
- {
- // If we have a woven class, mark it as complete.
- // Not exactly clear how we should deal with the
- // case where the weaving didn't happen because
- // someone else beat us in defining the class.
- if (wci != null)
- {
- wci.complete(clazz, bytes, wci.getDynamicImportsInternal());
- }
+ // At this point if we have a trigger class, then the deferred
+ // activation trigger has tripped.
+ if (!m_isActivationTriggered && isTriggerClass && (clazz != null))
+ {
+ m_isActivationTriggered = true;
+ }
+ }
+ }
+ finally
+ {
+ // If we have a woven class, mark it as complete.
+ // Not exactly clear how we should deal with the
+ // case where the weaving didn't happen because
+ // someone else beat us in defining the class.
+ if (wci != null)
+ {
+ wci.complete(clazz, bytes, wci.getDynamicImportsInternal());
+ }
- synchronized (lock)
- {
- m_classLocks.remove(name);
- lock.notifyAll();
- }
- }
- return clazz;
- }
+ synchronized (lock)
+ {
+ m_classLocks.remove(name);
+ lock.notifyAll();
+ }
+ }
+ return clazz;
+ }
- protected void transformClass(Felix felix, WovenClassImpl wci,
- Set<ServiceReference<WeavingHook>> hooks,
- Set<ServiceReference<WovenClassListener>> wovenClassListeners,
- String name, byte[] bytes) throws Error {
-
- // Loop through hooks in service ranking order.
- for (ServiceReference<WeavingHook> sr : hooks)
- {
- // Only use the hook if it is not black listed.
- if (!felix.isHookBlackListed(sr))
- {
- // Get the hook service object.
- // Note that we don't use the bundle context
- // to get the service object since that would
- // perform sercurity checks.
- WeavingHook wh = felix.getService(felix, sr, false);
- if (wh != null)
- {
- try
- {
- BundleRevisionImpl.getSecureAction()
- .invokeWeavingHook(wh, wci);
- }
- catch (Throwable th)
- {
- if (!(th instanceof WeavingException))
- {
- felix.blackListHook(sr);
- }
- felix.fireFrameworkEvent(
- FrameworkEvent.ERROR,
- sr.getBundle(),
- th);
+ void transformClass(Felix felix, WovenClassImpl wci,
+ Set<ServiceReference<WeavingHook>> hooks,
+ Set<ServiceReference<WovenClassListener>> wovenClassListeners,
+ String name, byte[] bytes) throws Error {
+
+ // Loop through hooks in service ranking order.
+ for (ServiceReference<WeavingHook> sr : hooks)
+ {
+ // Only use the hook if it is not black listed.
+ if (!felix.isHookBlackListed(sr))
+ {
+ // Get the hook service object.
+ // Note that we don't use the bundle context
+ // to get the service object since that would
+ // perform sercurity checks.
+ WeavingHook wh = felix.getService(felix, sr, false);
+ if (wh != null)
+ {
+ try
+ {
+ BundleRevisionImpl.getSecureAction()
+ .invokeWeavingHook(wh, wci);
+ }
+ catch (Throwable th)
+ {
+ if (!(th instanceof WeavingException))
+ {
+ felix.blackListHook(sr);
+ }
+ felix.fireFrameworkEvent(
+ FrameworkEvent.ERROR,
+ sr.getBundle(),
+ th);
- // Mark the woven class as incomplete.
- wci.complete(null, null, null);
- // Throw class format exception per spec.
- Error error = new ClassFormatError("Weaving hook failed.");
- error.initCause(th);
- throw error;
- }
- finally
- {
- felix.ungetService(felix, sr, null);
- }
- }
- }
- }
- wci.setState(WovenClass.TRANSFORMED);
- callWovenClassListeners(felix, wovenClassListeners, wci);
- }
+ // Mark the woven class as incomplete.
+ wci.complete(null, null, null);
+ // Throw class format exception per spec.
+ Error error = new ClassFormatError("Weaving hook failed.");
+ error.initCause(th);
+ throw error;
+ }
+ finally
+ {
+ felix.ungetService(felix, sr, null);
+ }
+ }
+ }
+ }
+ wci.setState(WovenClass.TRANSFORMED);
+ callWovenClassListeners(felix, wovenClassListeners, wci);
+ }
protected void callWovenClassListeners(Felix felix, Set<ServiceReference<WovenClassListener>> wovenClassListeners, WovenClass wovenClass)
{
- if(wovenClassListeners != null)
- {
- for(ServiceReference<WovenClassListener> currentWovenClassListenerRef : wovenClassListeners)
- {
- WovenClassListener currentWovenClassListner = felix.getService(felix, currentWovenClassListenerRef, false);
- try
- {
- BundleRevisionImpl.getSecureAction().invokeWovenClassListener(currentWovenClassListner, wovenClass);
- }
- catch (Exception e)
- {
- m_logger.log(Logger.LOG_ERROR, "Woven Class Listner failed.", e);
- }
- finally
- {
- felix.ungetService(felix, currentWovenClassListenerRef, null);
- }
- }
- }
+ if(wovenClassListeners != null)
+ {
+ for(ServiceReference<WovenClassListener> currentWovenClassListenerRef : wovenClassListeners)
+ {
+ WovenClassListener currentWovenClassListner = felix.getService(felix, currentWovenClassListenerRef, false);
+ try
+ {
+ BundleRevisionImpl.getSecureAction().invokeWovenClassListener(currentWovenClassListner, wovenClass);
+ }
+ catch (Exception e)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Woven Class Listner failed.", e);
+ }
+ finally
+ {
+ felix.ungetService(felix, currentWovenClassListenerRef, null);
+ }
+ }
+ }
}
private Object[] definePackage(String pkgName)
diff --git a/framework/src/main/java/org/apache/felix/framework/WovenClassImpl.java b/framework/src/main/java/org/apache/felix/framework/WovenClassImpl.java
index e9f1c74..b929cd3 100644
--- a/framework/src/main/java/org/apache/felix/framework/WovenClassImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/WovenClassImpl.java
@@ -42,7 +42,8 @@
private boolean m_isComplete = false;
private int m_state;
- /* package */ WovenClassImpl(String className, BundleWiring wiring, byte[] bytes)
+ /* package */WovenClassImpl(String className, BundleWiring wiring,
+ byte[] bytes)
{
m_className = className;
m_wiring = wiring;
@@ -50,13 +51,13 @@
m_state = TRANSFORMING;
}
- synchronized void complete(Class definedClass, byte[] bytes, List<String> imports)
+ synchronized void complete(Class definedClass, byte[] bytes,
+ List<String> imports)
{
m_definedClass = definedClass;
m_bytes = (bytes == null) ? m_bytes : bytes;
- m_imports = (imports == null)
- ? ImmutableList.newInstance(m_imports)
- : ImmutableList.newInstance(imports);
+ m_imports = (imports == null) ? ImmutableList.newInstance(m_imports)
+ : ImmutableList.newInstance(imports);
}
public synchronized byte[] getBytes()
@@ -64,7 +65,8 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
byte[] bytes = m_bytes;
if (m_isComplete)
@@ -80,14 +82,14 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
if (m_isComplete)
{
throw new IllegalStateException(
- "Cannot change bytes after class weaving is completed.");
- }
- else
+ "Cannot change bytes after class weaving is completed.");
+ } else
{
m_bytes = bytes;
}
@@ -105,7 +107,7 @@
public synchronized boolean isWeavingComplete()
{
- return m_isComplete;
+ return m_isComplete;
}
public String getClassName()
@@ -115,7 +117,8 @@
public ProtectionDomain getProtectionDomain()
{
- return ((BundleImpl) m_wiring.getRevision().getBundle()).getProtectionDomain();
+ return ((BundleImpl) m_wiring.getRevision().getBundle())
+ .getProtectionDomain();
}
public synchronized Class<?> getDefinedClass()
@@ -170,19 +173,19 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
if (s != null)
{
try
{
- List<BundleRequirement> reqs =
- ManifestParser.parseDynamicImportHeader(null, null, s);
- }
- catch (Exception ex)
+ List<BundleRequirement> reqs = ManifestParser
+ .parseDynamicImportHeader(null, null, s);
+ } catch (Exception ex)
{
- RuntimeException re =
- new IllegalArgumentException("Unable to parse dynamic import.");
+ RuntimeException re = new IllegalArgumentException(
+ "Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
@@ -196,7 +199,8 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
return m_imports.remove(o);
}
@@ -211,19 +215,19 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
for (String s : collection)
{
try
{
- List<BundleRequirement> reqs =
- ManifestParser.parseDynamicImportHeader(null, null, s);
- }
- catch (Exception ex)
+ List<BundleRequirement> reqs = ManifestParser
+ .parseDynamicImportHeader(null, null, s);
+ } catch (Exception ex)
{
- RuntimeException re =
- new IllegalArgumentException("Unable to parse dynamic import.");
+ RuntimeException re = new IllegalArgumentException(
+ "Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
@@ -231,24 +235,25 @@
return m_imports.addAll(collection);
}
- public synchronized boolean addAll(int i, Collection<? extends String> collection)
+ public synchronized boolean addAll(int i,
+ Collection<? extends String> collection)
{
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
for (String s : collection)
{
try
{
- List<BundleRequirement> reqs =
- ManifestParser.parseDynamicImportHeader(null, null, s);
- }
- catch (Exception ex)
+ List<BundleRequirement> reqs = ManifestParser
+ .parseDynamicImportHeader(null, null, s);
+ } catch (Exception ex)
{
- RuntimeException re =
- new IllegalArgumentException("Unable to parse dynamic import.");
+ RuntimeException re = new IllegalArgumentException(
+ "Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
@@ -261,7 +266,8 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
return m_imports.removeAll(collection);
}
@@ -271,7 +277,8 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
return m_imports.retainAll(collection);
}
@@ -291,17 +298,17 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
try
{
- List<BundleRequirement> reqs =
- ManifestParser.parseDynamicImportHeader(null, null, s);
- }
- catch (Exception ex)
+ List<BundleRequirement> reqs = ManifestParser
+ .parseDynamicImportHeader(null, null, s);
+ } catch (Exception ex)
{
- RuntimeException re =
- new IllegalArgumentException("Unable to parse dynamic import.");
+ RuntimeException re = new IllegalArgumentException(
+ "Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
@@ -313,17 +320,17 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
try
{
- List<BundleRequirement> reqs =
- ManifestParser.parseDynamicImportHeader(null, null, s);
- }
- catch (Exception ex)
+ List<BundleRequirement> reqs = ManifestParser
+ .parseDynamicImportHeader(null, null, s);
+ } catch (Exception ex)
{
- RuntimeException re =
- new IllegalArgumentException("Unable to parse dynamic import.");
+ RuntimeException re = new IllegalArgumentException(
+ "Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
@@ -335,7 +342,8 @@
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
- sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
+ sm.checkPermission(new AdminPermission(m_wiring.getBundle(),
+ AdminPermission.WEAVE));
}
return m_imports.remove(i);
}
@@ -376,24 +384,26 @@
return bytes;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.osgi.framework.hooks.weaving.WovenClass#getState()
*/
- public synchronized int getState()
+ public synchronized int getState()
{
return m_state;
}
-
+
public synchronized void setState(int state)
{
- //Per 56.6.4.13 Weaving complete if state is DEFINED, DEFINE_FAILED, or TRANSFORMING_FAILED
- if(!m_isComplete && (state == DEFINED ||
- state == DEFINE_FAILED ||
- state == TRANSFORMING_FAILED))
- {
- m_isComplete = true;
- }
- m_state = state;
+ // Per 56.6.4.13 Weaving complete if state is DEFINED, DEFINE_FAILED, or
+ // TRANSFORMING_FAILED
+ if (!m_isComplete
+ && (state == DEFINED || state == DEFINE_FAILED || state == TRANSFORMING_FAILED))
+ {
+ m_isComplete = true;
+ }
+ m_state = state;
}
}
\ No newline at end of file
diff --git a/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java b/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
index c39714c..cf570e5 100644
--- a/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
+++ b/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
@@ -49,368 +49,415 @@
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWire;
-public class BundleWiringImplTest
+public class BundleWiringImplTest
{
- private BundleWiringImpl bundleWiring;
-
- private StatefulResolver mockResolver;
-
- private BundleRevisionImpl mockRevisionImpl;
-
- private BundleImpl mockBundle;
-
- @SuppressWarnings("rawtypes")
- public void initializeSimpleBundleWiring() throws Exception
- {
-
- mockResolver = mock(StatefulResolver.class);
- mockRevisionImpl = mock(BundleRevisionImpl.class);
- mockBundle = mock(BundleImpl.class);
-
- Logger logger = new Logger();
- Map configMap = new HashMap();
- List<BundleRevision> fragments = new ArrayList<BundleRevision>();
- List<BundleWire> wires = new ArrayList<BundleWire>();
- Map<String, BundleRevision> importedPkgs = new HashMap<String, BundleRevision>();
- Map<String, List<BundleRevision>> requiredPkgs =
- new HashMap<String, List<BundleRevision>>();
-
- when(mockRevisionImpl.getBundle()).thenReturn(mockBundle);
- when(mockBundle.getBundleId()).thenReturn(Long.valueOf(1));
-
- bundleWiring = new BundleWiringImpl(logger, configMap, mockResolver, mockRevisionImpl,
- fragments, wires, importedPkgs, requiredPkgs);
- }
+ private BundleWiringImpl bundleWiring;
- @Test
- public void testBundleClassLoader() throws Exception
- {
- bundleWiring = mock(BundleWiringImpl.class);
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoader.class, bundleWiring);
- assertNotNull(bundleClassLoader);
- }
-
- @Test
- public void testBundleClassLoaderJava5() throws Exception
- {
- bundleWiring = mock(BundleWiringImpl.class);
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);
- assertNotNull(bundleClassLoader);
- }
-
- @SuppressWarnings("rawtypes")
- @Test
- public void testFindClassNonExistant() throws Exception
- {
- initializeSimpleBundleWiring();
-
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);
- assertNotNull(bundleClassLoader);
- Class foundClass = null;
- try
- {
- foundClass = bundleClassLoader.findClass("org.apache.felix.test.NonExistant");
- }
- catch (ClassNotFoundException e)
- {
- fail("Class should not throw exception");
- }
- assertNull("Nonexistant Class Should be null", foundClass);
- }
-
- @SuppressWarnings("rawtypes")
- @Test
- public void testFindClassExistant() throws Exception
- {
- Felix mockFramework = mock(Felix.class);
- Content mockContent = mock(Content.class);
- Class testClass = TestClass.class;
- String testClassName = testClass.getName();
- String testClassAsPath = testClassName.replace('.', '/') + ".class";
- byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
-
- List<Content> contentPath = new ArrayList<Content>();
- contentPath.add(mockContent);
- initializeSimpleBundleWiring();
-
- when(mockBundle.getFramework()).thenReturn(mockFramework);
- when(mockFramework.getBootPackages()).thenReturn(new String[0]);
-
- when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
- when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
-
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);
- assertNotNull(bundleClassLoader);
- Class foundClass = null;
- try
- {
-
- foundClass = bundleClassLoader.findClass(TestClass.class.getName());
- }
- catch (ClassNotFoundException e)
- {
- fail("Class should not throw exception");
- }
- assertNotNull("Class Should be found in this classloader", foundClass);
- }
-
- @SuppressWarnings({ "unchecked", "rawtypes" })
- @Test
- public void testFindClassWeave() throws Exception
- {
- Felix mockFramework = mock(Felix.class);
- Content mockContent = mock(Content.class);
- ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
- ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
-
- Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
- hooks.add(mockServiceReferenceWeavingHook);
-
- DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
-
- Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
- listeners.add(mockServiceReferenceWovenClassListener);
-
- Class testClass = TestClass.class;
- String testClassName = testClass.getName();
- String testClassAsPath = testClassName.replace('.', '/') + ".class";
- byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
-
- List<Content> contentPath = new ArrayList<Content>();
- contentPath.add(mockContent);
- initializeSimpleBundleWiring();
-
- when(mockBundle.getFramework()).thenReturn(mockFramework);
- when(mockFramework.getBootPackages()).thenReturn(new String[0]);
-
- when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
- when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
-
- when(mockFramework.getHooks(WeavingHook.class)).thenReturn(hooks);
- when(mockFramework.getService(mockFramework, mockServiceReferenceWeavingHook, false)).thenReturn(new GoodDummyWovenHook());
-
- when(mockFramework.getHooks(WovenClassListener.class)).thenReturn(listeners);
- when(mockFramework.getService(mockFramework, mockServiceReferenceWovenClassListener, false)).thenReturn(dummyWovenClassListener);
-
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);
- assertNotNull(bundleClassLoader);
- Class foundClass = null;
- try
- {
-
- foundClass = bundleClassLoader.findClass(TestClass.class.getName());
- }
- catch (ClassNotFoundException e)
- {
- fail("Class should not throw exception");
- }
- assertNotNull("Class Should be found in this classloader", foundClass);
- assertEquals("Weaving should have added a field", 1, foundClass.getFields().length);
- assertEquals("There should be 2 state changes fired by the weaving", 2, dummyWovenClassListener.stateList.size());
- assertEquals("The first state change should transform the class", WovenClass.TRANSFORMED, dummyWovenClassListener.stateList.get(0));
- assertEquals("The second state change should define the class", WovenClass.DEFINED, dummyWovenClassListener.stateList.get(1));
- }
-
- @SuppressWarnings({ "unchecked", "rawtypes" })
- @Test
- public void testFindClassBadWeave() throws Exception
- {
- Felix mockFramework = mock(Felix.class);
- Content mockContent = mock(Content.class);
- ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
- ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
-
- Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
- hooks.add(mockServiceReferenceWeavingHook);
+ private StatefulResolver mockResolver;
- DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
-
- Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
- listeners.add(mockServiceReferenceWovenClassListener);
-
- Class testClass = TestClass.class;
- String testClassName = testClass.getName();
- String testClassAsPath = testClassName.replace('.', '/') + ".class";
- byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
-
- List<Content> contentPath = new ArrayList<Content>();
- contentPath.add(mockContent);
- initializeSimpleBundleWiring();
-
- when(mockBundle.getFramework()).thenReturn(mockFramework);
- when(mockFramework.getBootPackages()).thenReturn(new String[0]);
-
- when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
- when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
-
- when(mockFramework.getHooks(WeavingHook.class)).thenReturn(hooks);
- when(mockFramework.getService(mockFramework, mockServiceReferenceWeavingHook, false)).thenReturn(new BadDummyWovenHook());
-
- when(mockFramework.getHooks(WovenClassListener.class)).thenReturn(listeners);
- when(mockFramework.getService(mockFramework, mockServiceReferenceWovenClassListener, false)).thenReturn(dummyWovenClassListener);
-
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);
- assertNotNull(bundleClassLoader);
-
- try {
-
- bundleClassLoader.findClass(TestClass.class.getName());
- fail("Class should throw exception");
- }
- catch (Error e)
- {
- //This is expected
- }
-
- assertEquals("There should be 1 state changes fired by the weaving", 1, dummyWovenClassListener.stateList.size());
- assertEquals("The only state change should be a failed transform on the class", WovenClass.TRANSFORMING_FAILED, dummyWovenClassListener.stateList.get(0));
-
- }
-
- @SuppressWarnings({ "unchecked", "rawtypes" })
- @Test
- public void testFindClassWeaveDefineError() throws Exception
- {
- Felix mockFramework = mock(Felix.class);
- Content mockContent = mock(Content.class);
- ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
- ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
-
- Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
- hooks.add(mockServiceReferenceWeavingHook);
-
- DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
-
- Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
- listeners.add(mockServiceReferenceWovenClassListener);
-
- Class testClass = TestClass.class;
- String testClassName = testClass.getName();
- String testClassAsPath = testClassName.replace('.', '/') + ".class";
- byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
-
- List<Content> contentPath = new ArrayList<Content>();
- contentPath.add(mockContent);
- initializeSimpleBundleWiring();
-
- when(mockBundle.getFramework()).thenReturn(mockFramework);
- when(mockFramework.getBootPackages()).thenReturn(new String[0]);
-
- when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
- when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);
-
- when(mockFramework.getHooks(WeavingHook.class)).thenReturn(hooks);
- when(mockFramework.getService(mockFramework, mockServiceReferenceWeavingHook, false)).thenReturn(new BadDefineWovenHook());
-
- when(mockFramework.getHooks(WovenClassListener.class)).thenReturn(listeners);
- when(mockFramework.getService(mockFramework, mockServiceReferenceWovenClassListener, false)).thenReturn(dummyWovenClassListener);
-
- BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);
- assertNotNull(bundleClassLoader);
- try
- {
-
- bundleClassLoader.findClass(TestClass.class.getName());
- }
- catch (ClassNotFoundException e)
- {
- fail("Class should not throw exception");
- }
- assertEquals("There should be 2 state changes fired by the weaving", 2, dummyWovenClassListener.stateList.size());
- assertEquals("The first state change should transform the class", WovenClass.TRANSFORMED, dummyWovenClassListener.stateList.get(0));
- assertEquals("The second state change failed the define on the class", WovenClass.DEFINE_FAILED, dummyWovenClassListener.stateList.get(1));
- }
-
- @SuppressWarnings("rawtypes")
- private byte[] createTestClassBytes(Class testClass, String testClassAsPath)
- throws IOException
- {
- InputStream testClassResourceStream =
- testClass.getClassLoader().getResourceAsStream(testClassAsPath);
-
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int curByte;
- while((curByte = testClassResourceStream.read()) != -1)
- {
- baos.write(curByte);
- }
- byte[] testClassBytes = baos.toByteArray();
- return testClassBytes;
- }
-
-
- @SuppressWarnings("rawtypes")
- private BundleClassLoader createBundleClassLoader(Class bundleClassLoaderClass, BundleWiringImpl bundleWiring) throws Exception
- {
- Logger logger = new Logger();
- Constructor ctor = BundleRevisionImpl.getSecureAction()
- .getConstructor(bundleClassLoaderClass, new Class[] { BundleWiringImpl.class, ClassLoader.class, Logger.class });
- BundleClassLoader bundleClassLoader = (BundleClassLoader)
- BundleRevisionImpl.getSecureAction().invoke(ctor,
- new Object[] { bundleWiring, this.getClass().getClassLoader(), logger });
+ private BundleRevisionImpl mockRevisionImpl;
+
+ private BundleImpl mockBundle;
+
+ @SuppressWarnings("rawtypes")
+ public void initializeSimpleBundleWiring() throws Exception
+ {
+
+ mockResolver = mock(StatefulResolver.class);
+ mockRevisionImpl = mock(BundleRevisionImpl.class);
+ mockBundle = mock(BundleImpl.class);
+
+ Logger logger = new Logger();
+ Map configMap = new HashMap();
+ List<BundleRevision> fragments = new ArrayList<BundleRevision>();
+ List<BundleWire> wires = new ArrayList<BundleWire>();
+ Map<String, BundleRevision> importedPkgs = new HashMap<String, BundleRevision>();
+ Map<String, List<BundleRevision>> requiredPkgs = new HashMap<String, List<BundleRevision>>();
+
+ when(mockRevisionImpl.getBundle()).thenReturn(mockBundle);
+ when(mockBundle.getBundleId()).thenReturn(Long.valueOf(1));
+
+ bundleWiring = new BundleWiringImpl(logger, configMap, mockResolver,
+ mockRevisionImpl, fragments, wires, importedPkgs, requiredPkgs);
+ }
+
+ @Test
+ public void testBundleClassLoader() throws Exception
+ {
+ bundleWiring = mock(BundleWiringImpl.class);
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoader.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+ }
+
+ @Test
+ public void testBundleClassLoaderJava5() throws Exception
+ {
+ bundleWiring = mock(BundleWiringImpl.class);
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoaderJava5.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void testFindClassNonExistant() throws Exception
+ {
+ initializeSimpleBundleWiring();
+
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoaderJava5.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+ Class foundClass = null;
+ try
+ {
+ foundClass = bundleClassLoader
+ .findClass("org.apache.felix.test.NonExistant");
+ } catch (ClassNotFoundException e)
+ {
+ fail("Class should not throw exception");
+ }
+ assertNull("Nonexistant Class Should be null", foundClass);
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void testFindClassExistant() throws Exception
+ {
+ Felix mockFramework = mock(Felix.class);
+ Content mockContent = mock(Content.class);
+ Class testClass = TestClass.class;
+ String testClassName = testClass.getName();
+ String testClassAsPath = testClassName.replace('.', '/') + ".class";
+ byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
+
+ List<Content> contentPath = new ArrayList<Content>();
+ contentPath.add(mockContent);
+ initializeSimpleBundleWiring();
+
+ when(mockBundle.getFramework()).thenReturn(mockFramework);
+ when(mockFramework.getBootPackages()).thenReturn(new String[0]);
+
+ when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
+ when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(
+ testClassBytes);
+
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoaderJava5.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+ Class foundClass = null;
+ try
+ {
+
+ foundClass = bundleClassLoader.findClass(TestClass.class.getName());
+ } catch (ClassNotFoundException e)
+ {
+ fail("Class should not throw exception");
+ }
+ assertNotNull("Class Should be found in this classloader", foundClass);
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Test
+ public void testFindClassWeave() throws Exception
+ {
+ Felix mockFramework = mock(Felix.class);
+ Content mockContent = mock(Content.class);
+ ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
+ ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
+
+ Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
+ hooks.add(mockServiceReferenceWeavingHook);
+
+ DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
+
+ Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
+ listeners.add(mockServiceReferenceWovenClassListener);
+
+ Class testClass = TestClass.class;
+ String testClassName = testClass.getName();
+ String testClassAsPath = testClassName.replace('.', '/') + ".class";
+ byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
+
+ List<Content> contentPath = new ArrayList<Content>();
+ contentPath.add(mockContent);
+ initializeSimpleBundleWiring();
+
+ when(mockBundle.getFramework()).thenReturn(mockFramework);
+ when(mockFramework.getBootPackages()).thenReturn(new String[0]);
+
+ when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
+ when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(
+ testClassBytes);
+
+ when(mockFramework.getHooks(WeavingHook.class)).thenReturn(hooks);
+ when(
+ mockFramework.getService(mockFramework,
+ mockServiceReferenceWeavingHook, false)).thenReturn(
+ new GoodDummyWovenHook());
+
+ when(mockFramework.getHooks(WovenClassListener.class)).thenReturn(
+ listeners);
+ when(
+ mockFramework.getService(mockFramework,
+ mockServiceReferenceWovenClassListener, false))
+ .thenReturn(dummyWovenClassListener);
+
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoaderJava5.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+ Class foundClass = null;
+ try
+ {
+
+ foundClass = bundleClassLoader.findClass(TestClass.class.getName());
+ } catch (ClassNotFoundException e)
+ {
+ fail("Class should not throw exception");
+ }
+ assertNotNull("Class Should be found in this classloader", foundClass);
+ assertEquals("Weaving should have added a field", 1,
+ foundClass.getFields().length);
+ assertEquals("There should be 2 state changes fired by the weaving", 2,
+ dummyWovenClassListener.stateList.size());
+ assertEquals("The first state change should transform the class",
+ WovenClass.TRANSFORMED,
+ dummyWovenClassListener.stateList.get(0));
+ assertEquals("The second state change should define the class",
+ WovenClass.DEFINED, dummyWovenClassListener.stateList.get(1));
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Test
+ public void testFindClassBadWeave() throws Exception
+ {
+ Felix mockFramework = mock(Felix.class);
+ Content mockContent = mock(Content.class);
+ ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
+ ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
+
+ Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
+ hooks.add(mockServiceReferenceWeavingHook);
+
+ DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
+
+ Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
+ listeners.add(mockServiceReferenceWovenClassListener);
+
+ Class testClass = TestClass.class;
+ String testClassName = testClass.getName();
+ String testClassAsPath = testClassName.replace('.', '/') + ".class";
+ byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
+
+ List<Content> contentPath = new ArrayList<Content>();
+ contentPath.add(mockContent);
+ initializeSimpleBundleWiring();
+
+ when(mockBundle.getFramework()).thenReturn(mockFramework);
+ when(mockFramework.getBootPackages()).thenReturn(new String[0]);
+
+ when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
+ when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(
+ testClassBytes);
+
+ when(mockFramework.getHooks(WeavingHook.class)).thenReturn(hooks);
+ when(
+ mockFramework.getService(mockFramework,
+ mockServiceReferenceWeavingHook, false)).thenReturn(
+ new BadDummyWovenHook());
+
+ when(mockFramework.getHooks(WovenClassListener.class)).thenReturn(
+ listeners);
+ when(
+ mockFramework.getService(mockFramework,
+ mockServiceReferenceWovenClassListener, false))
+ .thenReturn(dummyWovenClassListener);
+
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoaderJava5.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+
+ try
+ {
+
+ bundleClassLoader.findClass(TestClass.class.getName());
+ fail("Class should throw exception");
+ } catch (Error e)
+ {
+ // This is expected
+ }
+
+ assertEquals("There should be 1 state changes fired by the weaving", 1,
+ dummyWovenClassListener.stateList.size());
+ assertEquals(
+ "The only state change should be a failed transform on the class",
+ WovenClass.TRANSFORMING_FAILED,
+ dummyWovenClassListener.stateList.get(0));
+
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Test
+ public void testFindClassWeaveDefineError() throws Exception
+ {
+ Felix mockFramework = mock(Felix.class);
+ Content mockContent = mock(Content.class);
+ ServiceReference<WeavingHook> mockServiceReferenceWeavingHook = mock(ServiceReference.class);
+ ServiceReference<WovenClassListener> mockServiceReferenceWovenClassListener = mock(ServiceReference.class);
+
+ Set<ServiceReference<WeavingHook>> hooks = new HashSet<ServiceReference<WeavingHook>>();
+ hooks.add(mockServiceReferenceWeavingHook);
+
+ DummyWovenClassListener dummyWovenClassListener = new DummyWovenClassListener();
+
+ Set<ServiceReference<WovenClassListener>> listeners = new HashSet<ServiceReference<WovenClassListener>>();
+ listeners.add(mockServiceReferenceWovenClassListener);
+
+ Class testClass = TestClass.class;
+ String testClassName = testClass.getName();
+ String testClassAsPath = testClassName.replace('.', '/') + ".class";
+ byte[] testClassBytes = createTestClassBytes(testClass, testClassAsPath);
+
+ List<Content> contentPath = new ArrayList<Content>();
+ contentPath.add(mockContent);
+ initializeSimpleBundleWiring();
+
+ when(mockBundle.getFramework()).thenReturn(mockFramework);
+ when(mockFramework.getBootPackages()).thenReturn(new String[0]);
+
+ when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);
+ when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(
+ testClassBytes);
+
+ when(mockFramework.getHooks(WeavingHook.class)).thenReturn(hooks);
+ when(
+ mockFramework.getService(mockFramework,
+ mockServiceReferenceWeavingHook, false)).thenReturn(
+ new BadDefineWovenHook());
+
+ when(mockFramework.getHooks(WovenClassListener.class)).thenReturn(
+ listeners);
+ when(
+ mockFramework.getService(mockFramework,
+ mockServiceReferenceWovenClassListener, false))
+ .thenReturn(dummyWovenClassListener);
+
+ BundleClassLoader bundleClassLoader = createBundleClassLoader(
+ BundleClassLoaderJava5.class, bundleWiring);
+ assertNotNull(bundleClassLoader);
+ try
+ {
+
+ bundleClassLoader.findClass(TestClass.class.getName());
+ } catch (ClassNotFoundException e)
+ {
+ fail("Class should not throw exception");
+ }
+ assertEquals("There should be 2 state changes fired by the weaving", 2,
+ dummyWovenClassListener.stateList.size());
+ assertEquals("The first state change should transform the class",
+ WovenClass.TRANSFORMED,
+ dummyWovenClassListener.stateList.get(0));
+ assertEquals("The second state change failed the define on the class",
+ WovenClass.DEFINE_FAILED,
+ dummyWovenClassListener.stateList.get(1));
+ }
+
+ @SuppressWarnings("rawtypes")
+ private byte[] createTestClassBytes(Class testClass, String testClassAsPath)
+ throws IOException
+ {
+ InputStream testClassResourceStream = testClass.getClassLoader()
+ .getResourceAsStream(testClassAsPath);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ int curByte;
+ while ((curByte = testClassResourceStream.read()) != -1)
+ {
+ baos.write(curByte);
+ }
+ byte[] testClassBytes = baos.toByteArray();
+ return testClassBytes;
+ }
+
+ @SuppressWarnings("rawtypes")
+ private BundleClassLoader createBundleClassLoader(
+ Class bundleClassLoaderClass, BundleWiringImpl bundleWiring)
+ throws Exception
+ {
+ Logger logger = new Logger();
+ Constructor ctor = BundleRevisionImpl.getSecureAction().getConstructor(
+ bundleClassLoaderClass,
+ new Class[] { BundleWiringImpl.class, ClassLoader.class,
+ Logger.class });
+ BundleClassLoader bundleClassLoader = (BundleClassLoader) BundleRevisionImpl
+ .getSecureAction().invoke(
+ ctor,
+ new Object[] { bundleWiring,
+ this.getClass().getClassLoader(), logger });
return bundleClassLoader;
- }
-
-
- class TestClass
- {
- //An empty test class to weave.
- }
-
- class GoodDummyWovenHook implements WeavingHook {
- //Adds the awesomePublicField to a class
- @SuppressWarnings("unchecked")
- public void weave(WovenClass wovenClass)
- {
- byte[] wovenClassBytes = wovenClass.getBytes();
- ClassNode classNode = new ClassNode();
- ClassReader reader = new ClassReader(wovenClassBytes);
- reader.accept(classNode, 0);
- classNode.fields.add(
- new FieldNode(Opcodes.ACC_PUBLIC, "awesomePublicField", "Ljava/lang/String;", null, null));
- ClassWriter writer = new ClassWriter(reader, Opcodes.ASM4);
- classNode.accept(writer);
- wovenClass.setBytes(writer.toByteArray());
- }
- }
-
- class BadDefineWovenHook implements WeavingHook {
- //Adds the awesomePublicField twice to the class. This is bad java.
- @SuppressWarnings("unchecked")
- public void weave(WovenClass wovenClass)
- {
- byte[] wovenClassBytes = wovenClass.getBytes();
- ClassNode classNode = new ClassNode();
- ClassReader reader = new ClassReader(wovenClassBytes);
- reader.accept(classNode, 0);
- classNode.fields.add(
- new FieldNode(Opcodes.ACC_PUBLIC, "awesomePublicField", "Ljava/lang/String;", null, null));
- classNode.fields.add(
- new FieldNode(Opcodes.ACC_PUBLIC, "awesomePublicField", "Ljava/lang/String;", null, null));
- ClassWriter writer = new ClassWriter(reader, Opcodes.ASM4);
- classNode.accept(writer);
- wovenClass.setBytes(writer.toByteArray());
- }
- }
-
- class BadDummyWovenHook implements WeavingHook
- {
- //Just Blow up
- public void weave(WovenClass wovenClass)
- {
- throw new WeavingException("Bad Weaver!");
- }
- }
-
- class DummyWovenClassListener implements WovenClassListener
- {
- public List<Integer> stateList = new ArrayList<Integer>();
-
- public void modified(WovenClass wovenClass)
- {
- stateList.add(wovenClass.getState());
- }
- }
+ }
+
+ class TestClass
+ {
+ // An empty test class to weave.
+ }
+
+ class GoodDummyWovenHook implements WeavingHook
+ {
+ // Adds the awesomePublicField to a class
+ @SuppressWarnings("unchecked")
+ public void weave(WovenClass wovenClass)
+ {
+ byte[] wovenClassBytes = wovenClass.getBytes();
+ ClassNode classNode = new ClassNode();
+ ClassReader reader = new ClassReader(wovenClassBytes);
+ reader.accept(classNode, 0);
+ classNode.fields.add(new FieldNode(Opcodes.ACC_PUBLIC,
+ "awesomePublicField", "Ljava/lang/String;", null, null));
+ ClassWriter writer = new ClassWriter(reader, Opcodes.ASM4);
+ classNode.accept(writer);
+ wovenClass.setBytes(writer.toByteArray());
+ }
+ }
+
+ class BadDefineWovenHook implements WeavingHook
+ {
+ // Adds the awesomePublicField twice to the class. This is bad java.
+ @SuppressWarnings("unchecked")
+ public void weave(WovenClass wovenClass)
+ {
+ byte[] wovenClassBytes = wovenClass.getBytes();
+ ClassNode classNode = new ClassNode();
+ ClassReader reader = new ClassReader(wovenClassBytes);
+ reader.accept(classNode, 0);
+ classNode.fields.add(new FieldNode(Opcodes.ACC_PUBLIC,
+ "awesomePublicField", "Ljava/lang/String;", null, null));
+ classNode.fields.add(new FieldNode(Opcodes.ACC_PUBLIC,
+ "awesomePublicField", "Ljava/lang/String;", null, null));
+ ClassWriter writer = new ClassWriter(reader, Opcodes.ASM4);
+ classNode.accept(writer);
+ wovenClass.setBytes(writer.toByteArray());
+ }
+ }
+
+ class BadDummyWovenHook implements WeavingHook
+ {
+ // Just Blow up
+ public void weave(WovenClass wovenClass)
+ {
+ throw new WeavingException("Bad Weaver!");
+ }
+ }
+
+ class DummyWovenClassListener implements WovenClassListener
+ {
+ public List<Integer> stateList = new ArrayList<Integer>();
+
+ public void modified(WovenClass wovenClass)
+ {
+ stateList.add(wovenClass.getState());
+ }
+ }
}