View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.impl.lifecycle;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
21  import org.kuali.rice.core.api.lifecycle.Lifecycle;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  
24  /**
25   * A lifecycle that wraps a service.  This fetches and calls a lifecycle available
26   * in the GRL and calls lifecycle methods on that.
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class ServiceDelegatingLifecycle extends BaseLifecycle {
31  	
32  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BaseLifecycle.class);
33  
34  	private QName serviceName;
35  
36  	public ServiceDelegatingLifecycle(QName serviceName) {
37  		this.serviceName = serviceName;
38  	}
39  
40  	public ServiceDelegatingLifecycle(String serviceName) {
41  		this(new QName(serviceName));
42  	}
43  
44  	public void start() throws Exception {
45  	    if (!isStarted()) {
46  	        loadService(this.serviceName).start();
47  	    }
48  		super.start();
49  	}
50  
51  	public void stop() throws Exception {
52  	    if (isStarted()) {
53  	    	try {
54  	    		Lifecycle lifecycle = loadService(this.serviceName);
55  	    		if (lifecycle == null) {
56  	    			LOG.warn("Couldn't stop service, failed to locate service with name " + serviceName);
57  	    		} else {
58  	    			lifecycle.stop();
59  	    		}
60  	    	} catch (Exception e) {
61  	    		LOG.warn("couldn't stop service " + this.serviceName);
62  	    		throw e;
63  	    	}
64  	    }
65  		super.stop();
66  	}
67  
68  	protected Lifecycle loadService(QName serviceName) {
69  	    Object service = GlobalResourceLoader.getService(serviceName);
70  	    if (service == null) {
71  	        return null;
72  	    }
73  	    if (!(service instanceof Lifecycle)) {
74  	        throw new RuntimeException("Service with name " + serviceName + " does not implement the Lifecycle interface!");
75  	    }
76  	    return (Lifecycle) service;
77  	}
78  
79  }