use of org.osgi.framework.Bundle in project jersey by jersey.
the class ExtendedWadlWebappOsgiTest method testWadlOptionsMethod.
@Test
public void testWadlOptionsMethod() throws Exception {
// TODO - temporary workaround
// This is a workaround related to issue JERSEY-2093; grizzly (1.9.5) needs to have the correct context
// class loader set
ClassLoader myClassLoader = this.getClass().getClassLoader();
for (Bundle bundle : bundleContext.getBundles()) {
if ("webapp".equals(bundle.getSymbolicName())) {
myClassLoader = bundle.loadClass("org.glassfish.jersey.examples.extendedwadl.resources.MyApplication").getClassLoader();
break;
}
}
Thread.currentThread().setContextClassLoader(myClassLoader);
// END of workaround; The entire block can be removed after grizzly is migrated to more recent version
final ResourceConfig resourceConfig = createResourceConfig();
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
final Client client = ClientBuilder.newClient();
String wadl = client.target(baseUri).path("items").queryParam(WadlUtils.DETAILED_WADL_QUERY_PARAM, "true").request(MediaTypes.WADL_TYPE).options(String.class);
assertTrue("Generated wadl is of null length", !wadl.isEmpty());
assertTrue("Generated wadl doesn't contain the expected text", wadl.contains("This is a paragraph"));
checkWadl(wadl, baseUri);
server.shutdownNow();
}
use of org.osgi.framework.Bundle in project jersey by jersey.
the class ExtendedWadlWebappOsgiTest method testExtendedWadl.
/**
* Test checks that the WADL generated using the WadlGenerator api doesn't
* contain the expected text.
*
* @throws java.lang.Exception in case of a test error.
*/
@Test
public void testExtendedWadl() throws Exception {
// TODO - temporary workaround
// This is a workaround related to issue JERSEY-2093; grizzly (1.9.5) needs to have the correct context
// class loader set
ClassLoader myClassLoader = this.getClass().getClassLoader();
for (Bundle bundle : bundleContext.getBundles()) {
if ("webapp".equals(bundle.getSymbolicName())) {
myClassLoader = bundle.loadClass("org.glassfish.jersey.examples.extendedwadl.resources.MyApplication").getClassLoader();
break;
}
}
Thread.currentThread().setContextClassLoader(myClassLoader);
// END of workaround - the entire block can be deleted after grizzly is updated to recent version
// List all the OSGi bundles
StringBuilder sb = new StringBuilder();
sb.append("-- Bundle list -- \n");
for (Bundle b : bundleContext.getBundles()) {
sb.append(String.format("%1$5s", "[" + b.getBundleId() + "]")).append(" ").append(String.format("%1$-70s", b.getSymbolicName())).append(" | ").append(String.format("%1$-20s", b.getVersion())).append(" |");
try {
b.start();
sb.append(" STARTED | ");
} catch (BundleException e) {
sb.append(" *FAILED* | ").append(e.getMessage());
}
sb.append(b.getLocation()).append("\n");
}
sb.append("-- \n\n");
LOGGER.fine(sb.toString());
final ResourceConfig resourceConfig = createResourceConfig();
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
final Client client = ClientBuilder.newClient();
final Response response = client.target(baseUri).path("application.wadl").request(MediaTypes.WADL_TYPE).buildGet().invoke();
String wadl = response.readEntity(String.class);
LOGGER.info("RESULT = " + wadl);
assertTrue("Generated wadl is of null length", !wadl.isEmpty());
assertTrue("Generated wadl doesn't contain the expected text", wadl.contains("This is a paragraph"));
assertFalse(wadl.contains("application.wadl/xsd0.xsd"));
server.shutdownNow();
}
use of org.osgi.framework.Bundle in project cucumber-jvm by cucumber.
the class OsgiClassFinder method getDescendants.
@Override
public <T> Collection<Class<? extends T>> getDescendants(Class<T> parentType, String packageName) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Looking for sub classes of " + parentType.getName() + " in '" + packageName + "' package");
final String searchPath = packageName.replace('.', '/');
final ArrayList<Class<? extends T>> result = new ArrayList<Class<? extends T>>();
;
for (Bundle bundle : bundleContext.getBundles()) {
try {
result.addAll(findClassesInBundle(bundle, searchPath, parentType));
} catch (Exception e) {
LOGGER.error("Failed to inspect bundle " + bundle.getSymbolicName() + ": " + e.getMessage(), e);
}
}
result.trimToSize();
return result;
}
use of org.osgi.framework.Bundle in project openhab1-addons by openhab.
the class SmarthomaticBinding method activate.
/**
* activate binding
*
*/
@Override
public void activate() {
// log activate of binding
if (baseStation != null) {
logger.info("Smarthomatic Binding activated. BaseStation= {}", baseStation.toString());
}
Bundle bundle = SmarthomaticActivator.getContext().getBundle();
URL fileURL = bundle.getEntry("packet_layout.xml");
Packet packet = null;
try {
InputStream inputStream = fileURL.openConnection().getInputStream();
JAXBContext jaxbContext = JAXBContext.newInstance(Packet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
packet = (Packet) jaxbUnmarshaller.unmarshal(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
this.packet = packet;
}
use of org.osgi.framework.Bundle in project openhab1-addons by openhab.
the class EBusBinding method updated.
/*
* (non-Javadoc)
*
* @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
*/
@Override
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
logger.info("Update eBus Binding configuration ...");
if (properties == null || properties.isEmpty()) {
throw new RuntimeException("No properties in openhab.cfg set!");
}
try {
// stop last connector-thread if active
stopConnector();
// check to ensure that it is available
checkConfigurationProvider();
// clear current configuration
configurationProvider.clear();
// load parser from default url
parser = new EBusTelegramParser(configurationProvider);
URL configurationUrl = null;
String parsers = (String) properties.get("parsers");
if (StringUtils.isEmpty(parsers)) {
// set to current stable configurations as default
parsers = "common";
}
for (String elem : parsers.split(",")) {
configurationUrl = null;
// check for keyword custom to load custom configuration
if (elem.trim().equals("custom")) {
String parserUrl = (String) properties.get("parserUrl");
if (parserUrl != null) {
logger.debug("Load custom eBus Parser with url {}", parserUrl);
configurationUrl = new URL(parserUrl);
}
} else {
logger.debug("Load eBus Parser Configuration \"{}\" ...", elem.trim());
String filename = "src/main/resources/" + elem.trim() + "-configuration.json";
Bundle bundle = FrameworkUtil.getBundle(EBusBinding.class);
configurationUrl = bundle.getResource(filename);
if (configurationUrl == null) {
logger.error("Unable to load file {} ...", elem.trim() + "-configuration.json");
}
}
if (configurationUrl != null) {
configurationProvider.loadConfigurationFile(configurationUrl);
}
}
// check minimal config
if (properties.get("serialPort") != null && properties.get("hostname") != null) {
throw new ConfigurationException("hostname", "Set property serialPort or hostname, not both!");
}
// use the serial connector
if (StringUtils.isNotEmpty((String) properties.get("serialPort"))) {
try {
// load class by reflection to keep gnu.io (serial) optional. Declarative Services causes an
// class not found exception, also if serial is not used!
// FIXME: Is there a better way to avoid that a class not found exception?
@SuppressWarnings("unchecked") Class<AbstractEBusWriteConnector> _tempClass = (Class<AbstractEBusWriteConnector>) EBusBinding.class.getClassLoader().loadClass("org.openhab.binding.ebus.internal.connection.EBusSerialConnector");
Constructor<AbstractEBusWriteConnector> constructor = _tempClass.getDeclaredConstructor(String.class);
connector = constructor.newInstance((String) properties.get("serialPort"));
} catch (ClassNotFoundException e) {
logger.error(e.toString(), e);
} catch (NoSuchMethodException e) {
logger.error(e.toString(), e);
} catch (SecurityException e) {
logger.error(e.toString(), e);
} catch (InstantiationException e) {
logger.error(e.toString(), e);
} catch (IllegalAccessException e) {
logger.error(e.toString(), e);
} catch (IllegalArgumentException e) {
logger.error(e.toString(), e);
} catch (InvocationTargetException e) {
logger.error(e.toString(), e);
}
} else if (StringUtils.isNotEmpty((String) properties.get("hostname"))) {
// use the tcp-ip connector
connector = new EBusTCPConnector((String) properties.get("hostname"), Integer.parseInt((String) properties.get("port")));
}
// Set eBus sender id or default 0xFF
if (StringUtils.isNotEmpty((String) properties.get("senderId"))) {
connector.setSenderId(EBusUtils.toByte((String) properties.get("senderId")));
}
if (properties.get("record") != null) {
String debugWriterMode = (String) properties.get("record");
logger.info("Enable CSV writer for eBUS {}", debugWriterMode);
debugWriter = new EBusTelegramCSVWriter();
debugWriter.openInUserData("ebus-" + debugWriterMode + ".csv");
parser.setDebugCSVWriter(debugWriter, debugWriterMode);
}
// add event listener
connector.addEBusEventListener(this);
// start thread
connector.start();
// set the new connector
commandProcessor.setConnector(connector);
commandProcessor.setConfigurationProvider(configurationProvider);
} catch (MalformedURLException e) {
logger.error(e.toString(), e);
} catch (IOException e) {
throw new ConfigurationException("general", e.toString(), e);
}
}
Aggregations