Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
JOnAS: Java(TM) Open Application Server Copyright (C) 2008 Bull S.A.S. Contact: jonas-team@ow2.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------- $Id: CamelService.java 20427 2010-10-07 11:34:42Z alitokmen $ --------------------------------------------------------------------------
 
 package org.ow2.jonas.camel.service.impl;
 
 import java.util.List;
 import java.util.Map;
 
Camel service.

Author(s):
Guillaume Renault
 
 public class CamelService implements ICamelService {

    
Logger.
 
     private final Log logger = LogFactory.getLog(CamelService.class);

    
Map of all of the camel contexts.
 
     private Map<StringICamelWrappercamelContexts = null;

    
 
 
     private BundleContext bundleContext;

    
Constructor, needed to get the bundle context.

Parameters:
context the bundle context.
 
     public CamelService(final BundleContext context) {
         this. = context;
     }

    
Initializes the service.
 
     public void start() {
         this. = new HashMap<StringICamelWrapper>();
 
         this..info("Camel service started");
 
         if (this. != null) {
             CamelContextServiceFactory camelContextServiceFactory = new CamelContextServiceFactory();
             camelContextServiceFactory.setCamelService(this);
                 camelContextServiceFactorynull);
 
             this..info("Camel context factory service started");
         }
     }

    
Stops all Camel contexts.
    public void stop() {
        if (this. != null) {
            this..info("Camel context factory service stopped");
        }
        final List<StringstoppedContexts = new ArrayList<String>();
        final Map<StringICamelWrappercamelContexts = new HashMap<StringICamelWrapper>(this.);
        this. = new HashMap<StringICamelWrapper>();
        for (final Map.Entry<StringICamelWrapperentry : camelContexts.entrySet()) {
            try {
                entry.getValue().stopContext();
                stoppedContexts.add(entry.getKey());
                this..debug("The camel context {0} has been stopped."entry.getKey());
            } catch (final Exception e) {
                this..warn("Cannot stop the camel context {0}."entry.getKey(), e);
            }
        }
        for (final String stoppedContext : stoppedContexts) {
            camelContexts.remove(stoppedContext);
        }
        if (camelContexts.size() != 0) {
            this..warn("The Camel service still has {0} running contexts!"camelContexts.size());
        }
        this..info("Camel service stopped");
    }

    
Get the names of Camel contexts managed by the service.

Returns:
Names of Camel contexts managed by the service.
    public List<StringgetContextNames() {
        return new ArrayList<String>(this..keySet());
    }

    
Start a new camel context.

Returns:
the name of the camel context.
    public String startNewContext() throws Exception {
        this..info("Starting a new camel context");
        final CamelWrapper camelWrapper = new CamelWrapper(this.);
        // put the Camel wrapper in the wrappers list.
        this..put(camelWrapper.getCamelContextName(), camelWrapper);
        // set the wrapper's name
        camelWrapper.setName(camelWrapper.getCamelContextName());
        // start the context.
        camelWrapper.run();
        this..debug("A new Camel context has been created with the name {0}"camelWrapper.getCamelContextName());
        return camelWrapper.getCamelContextName();
    }

    
Stop an instance of Camel.

Parameters:
name the camel context name to stop.
Throws:
java.lang.Exception If stopping any CAMEL console fails.
    public void stop(final String namethrows Exception {
        if (this..containsKey(name)) {
            this..get(name).stopContext();
            this..remove(name);
            this..debug("The camel context {0} has been stopped."name);
        }
    }

    
Get an instance of Camel.

Parameters:
name the camel context name
Returns:
the camel context
    public CamelContext getCamelContext(final String name) {
        if (this..containsKey(name)) {
            final ICamelWrapper camelWrapper = this..get(name);
            return camelWrapper.getCamelContext();
        }
        return null;
    }

    
Get a simple registry instance of Camel context.

Parameters:
name the camel context name
Returns:
the registry instance
    public SimpleRegistry getSimpleRegistryOfCamelContext(final String name) {
        if (this..containsKey(name)) {
            final ICamelWrapper camelWrapper = this..get(name);
            return camelWrapper.getRegistryWrapper().getRegistry();
        }
        return null;
    }

    
Start a new camel context and return context.

Returns:
the camel context.
Throws:
java.lang.Exception If starting the context fails.
    public CamelContext startAndGetNewContext() throws Exception {
        final String contextName = this.startNewContext();
        return this.getCamelContext(contextName);
    }

    

Parameters:
builder the route to add.
camelContextName the camel context name on which the route has to be set.
Throws:
java.lang.Exception If adding route fails.
    public void addRoutes(final RouteBuilder builderfinal String camelContextNamethrows Exception {
        this..get(camelContextName).addRoutes(builder);
        this..debug("A route has been added on the following Camel context : {0}"camelContextName);
    }

    
Get the list of existing routes.

Parameters:
camelContextName the identifier of the camel context to check.
Returns:
the list of existing routes for the given camel context..
    public List<RouteDefinitiongetRouteDefinitions(final String camelContextName) {
        return this..get(camelContextName).getRouteDefinitions();
    }

    
Get a producer template for the given camel context.

Parameters:
camelContextName the camel context name on which the producer has to be taken.
Returns:
the producer template.
    public ProducerTemplate getProducerTemplate(final String camelContextName) {
        this..debug("Create a new producer template on the following Camel context : {0}"camelContextName);
        return this..get(camelContextName).getProducerTemplate();
    }

    
Add values to the registry component.

Parameters:
input the stream that contains the entries to add.
camelContextName the camel context name that contains the registry to update.
    public void addRegistry(final InputStream inputfinal String camelContextNamethrows Exception {
        this..debug("Add entries to the Registry component of the following Camel context : {0}"camelContextName);
        this..get(camelContextName).addToTheRegistry(input);
    }

    
Remove entries from the registry component.

Parameters:
input the input stream that contains all the entries to remove
camelContextName the camel context name to remove the entries on.
    public void removeRegistry(final InputStream inputfinal String camelContextName) {
        this..get(camelContextName).removeFromTheRegistry(input);
        this.
            .debug("Some entries were removed from the registry of the following camel context : {0}."camelContextName);
    }

    
Add a component on the given camel context.

Parameters:
componentName the component name.
component the component.
camelContextName the camel name context.
    public void addComponent(final String componentNamefinal Component componentfinal String camelContextName) {
        // register the component on the camel context.
        this..get(camelContextName).addComponent(componentNamecomponent);
        this..debug("Component {0} added on the Camel context {1}"componentNamecamelContextName);
    }
New to GrepCode? Check out our FAQ X