Created
September 7, 2011 16:42
-
-
Save atoulme/1201068 to your computer and use it in GitHub Desktop.
osgi_and_jruby.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OSGi is supported by JRuby 1.6.0 onwards. | |
1. OSGiScriptingContainer | |
Check out that class. Any OSGi app must use this container. In particular, you can add bundles to the jruby classpath easily with this container. | |
2. LocalScopeContext | |
By default it is set to SINGLETHREAD. That's the only one that works right now, others will spawn CNFE randomly when used. | |
There is a trick with the LocalScopeContext. Most of the local scope variables can harm you. So you can override the default local scope context to make it thread local, but that's quite expensive on the CPU (this was written by Federico Feller from my team): | |
public class MyOSGiScriptingContainer extends OSGiScriptingContainer { | |
private final ThreadLocal<LocalContext> contextHolder = new ThreadLocal<LocalContext>() { | |
@Override | |
public LocalContext initialValue() { | |
return getInstance(); | |
} | |
}; | |
public MyOSGiScriptingContainer(Bundle creator, LocalContextScope scope, | |
LocalVariableBehavior behavior, boolean lazy) { | |
super(creator, scope, behavior, lazy); | |
} | |
public MyOSGiScriptingContainer(Bundle creator, LocalContextScope scope, | |
LocalVariableBehavior behavior) { | |
super(creator, scope, behavior); | |
} | |
public MyOSGiScriptingContainer(Bundle creator) { | |
super(creator); | |
} | |
@Override | |
public BiVariableMap getVarMap() { | |
return contextHolder.get().getVarMap(getProvider().getRuntime()); | |
} | |
@Override | |
public Object get(String key) { | |
return getVarMap().get(getProvider().getRuntime().getTopSelf(), key); | |
} | |
@Override | |
public Object put(String key, Object value) { | |
return getVarMap().put(getProvider().getRuntime(), key, value); | |
} | |
@Override | |
public Object put(Object receiver, String key, Object value) { | |
return getVarMap().put(receiver, key, value); | |
} | |
@Override | |
public Object get(Object receiver, String key) { | |
return getVarMap().get(receiver, key); | |
} | |
@Override | |
public Object remove(Object receiver, String key) { | |
return getVarMap().remove(receiver, key); | |
} | |
@Override | |
public void clear() { | |
getVarMap().clear(); | |
} | |
@Override | |
public Map getAttributeMap() { | |
return contextHolder.get().getAttributeMap(); | |
} | |
@Override | |
public Object getAttribute(Object key) { | |
return getAttributeMap().get(key); | |
} | |
@Override | |
public Object setAttribute(Object key, Object value) { | |
return getAttributeMap().put(key, value); | |
} | |
@Override | |
public Object removeAttribute(Object key) { | |
return getAttributeMap().remove(key); | |
} | |
protected LocalContext getInstance() { | |
return new LocalContext(getProvider().getRuntime().getInstanceConfig(), | |
LocalVariableBehavior.TRANSIENT, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment