Search in sources :

Example 1 with AdminServiceMBean

use of org.apache.karaf.admin.management.AdminServiceMBean in project fabric8 by jboss-fuse.

the class ChildContainerProvider method createKarafContainerController.

private ChildContainerController createKarafContainerController() {
    return new ChildContainerController() {

        @Override
        public CreateChildContainerMetadata create(final CreateChildContainerOptions options, final CreationStateListener listener) {
            final Container parent = fabricService.get().getContainer(options.getParent());
            ContainerTemplate containerTemplate = new ContainerTemplate(parent, options.getJmxUser(), options.getJmxPassword(), false);
            return containerTemplate.execute(new ContainerTemplate.AdminServiceCallback<CreateChildContainerMetadata>() {

                public CreateChildContainerMetadata doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    return doCreateKaraf(adminService, options, listener, parent);
                }
            });
        }

        @Override
        public void start(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    // update jvm options if they have have changed
                    CreateChildContainerMetadata metadata = (CreateChildContainerMetadata) container.getMetadata();
                    CreateChildContainerOptions createOptions = metadata.getCreateOptions();
                    String jvmOpts = createOptions.getJvmOpts();
                    TabularData instances = adminService.getInstances();
                    Collection<CompositeDataSupport> values = (Collection<CompositeDataSupport>) instances.values();
                    for (CompositeDataSupport o : values) {
                        if (container.getId().equals(o.get("Name"))) {
                            if (o.containsKey("JavaOpts")) {
                                String oldJavaOpts = (String) o.get("JavaOpts");
                                StringBuilder stringBuilder = ChildContainerProvider.buildJvmOpts(createOptions, fabricService.get());
                                String extendendJvmOpts = stringBuilder.toString();
                                if (jvmOpts != null && !extendendJvmOpts.equals(oldJavaOpts)) {
                                    adminService.changeJavaOpts(container.getId(), extendendJvmOpts);
                                }
                            }
                            break;
                        }
                    }
                    adminService.startInstance(container.getId(), null);
                    return null;
                }
            });
        }

        @Override
        public void stop(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    String prevProvisionResult = container.getProvisionResult();
                    container.setProvisionResult(Container.PROVISION_STOPPING);
                    try {
                        adminService.stopInstance(container.getId());
                        container.setProvisionResult(Container.PROVISION_STOPPED);
                    } catch (Throwable t) {
                        container.setProvisionResult(prevProvisionResult);
                        LOG.error("Failed to stop container: " + container.getId(), t);
                        throw t;
                    }
                    return null;
                }
            });
        }

        @Override
        public void destroy(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    adminService.destroyInstance(container.getId());
                    return null;
                }
            });
        }
    };
}
Also used : CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) AdminServiceMBean(org.apache.karaf.admin.management.AdminServiceMBean) TabularData(javax.management.openmbean.TabularData) ContainerTemplate(io.fabric8.service.ContainerTemplate) Collection(java.util.Collection)

Aggregations

ContainerTemplate (io.fabric8.service.ContainerTemplate)1 Collection (java.util.Collection)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 TabularData (javax.management.openmbean.TabularData)1 AdminServiceMBean (org.apache.karaf.admin.management.AdminServiceMBean)1