/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.muse.core.platform.osgi.mini;
import java.util.Hashtable;
import org.apache.muse.core.platform.osgi.mini.internal.ResourceManagementAdminServiceImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator, ServiceListener {
private BundleContext context;
private ServiceReference httpServiceReference = null;
private ResourceManagementAdminServiceImpl adminService;
private boolean enabled = false;
private static final String HTTP_SERVICE_FILTER = "(objectclass=org.osgi.service.http.HttpService)";
public void start(BundleContext context) throws Exception {
this.context = context;
try {
if ((httpServiceReference = context
.getServiceReference("org.osgi.service.http.HttpService")) != null) {
adminService = new ResourceManagementAdminServiceImpl(context, httpServiceReference);
context.registerService(
"org.apache.muse.core.platform.osgi.ResourceManagementProvider",
adminService, new Hashtable());
enabled = true;
}
context.addServiceListener(this, HTTP_SERVICE_FILTER);
} catch (Exception e) {
e.printStackTrace();
throw new BundleException("Failed to start server");
}
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
if(httpServiceReference != null){
context.ungetService(httpServiceReference);
httpServiceReference = null;
}
}
public void serviceChanged(ServiceEvent event) {
ServiceReference ref = event.getServiceReference();
switch (event.getType()) {
case ServiceEvent.REGISTERED:
if (!enabled) {
try {
if ((httpServiceReference = context
.getServiceReference("org.osgi.service.http.HttpService")) != null) {
adminService = new ResourceManagementAdminServiceImpl(context, httpServiceReference);
context.registerService(
"org.apache.muse.core.platform.osgi.ResourceManagementProvider",
adminService, new Hashtable());
enabled = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case ServiceEvent.MODIFIED:
break;
case ServiceEvent.UNREGISTERING:
if (ref == httpServiceReference) {
enabled = false;
}
break;
}
}
}