diff --git a/LICENSE b/LICENSE
new file mode 100644
index 00000000..6b0b1270
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,203 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
+
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 00000000..b56c2784
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,6 @@
+Apache XBean
+Copyright 2005-2012 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
diff --git a/classpath/m2-readme.txt b/classpath/m2-readme.txt
deleted file mode 100644
index 3750464a..00000000
--- a/classpath/m2-readme.txt
+++ /dev/null
@@ -1,9 +0,0 @@
-Trying Maven 2.x
-----------------
-
-If you want to try m2 then simply run the mavenizer.sh script which
-will produce an "m2" directory. Move into the "m2" directory and
-
-m2 install
-
-That should compile, test, package and install
diff --git a/classpath/maven.xml b/classpath/maven.xml
deleted file mode 100644
index 0872ff3b..00000000
--- a/classpath/maven.xml
+++ /dev/null
@@ -1,34 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/classpath/mavenizer.sh b/classpath/mavenizer.sh
deleted file mode 100755
index 0c9696c3..00000000
--- a/classpath/mavenizer.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-DIR=m2
-
-rm -rf $DIR > /dev/null 2>&1
-mkdir $DIR
-
-cp -r src $DIR
-cp pom.xml $DIR
-
-(
- cd $DIR
-
- find . -name '\.svn' -exec rm -rf {} \;
-
- (
- cd src
-
- mkdir main ; mv java main
-
- ( cd test ; mkdir java ; mv org java )
-
- )
-
-)
diff --git a/classpath/pom.xml b/classpath/pom.xml
deleted file mode 100644
index dabacde2..00000000
--- a/classpath/pom.xml
+++ /dev/null
@@ -1,98 +0,0 @@
-
-
-
-
- 4.0.0
-
- XBean :: ClassPath
- xbean-classpath
- xbean
- 2.0-alpha-1-SNAPSHOT
-
- XBean
- http://xbean.org
-
-
-
-
- openejb
- OpenEJB Repo
- http://www.openejb.org/maven
- legacy
-
-
-
- http://www.xbean.org/
-
-
-
- repo1
- Maven Central Repository
- scp://repo1.maven.org/home/projects/maven/repository-staging/to-ibiblio/maven2
-
-
- website
- scp://minotaur.apache.org/www/maven.apache.org/maven2/
-
-
-
-
-
- xbean developers
- mailto:dev-subscribe@xbean.org
- mailto:dev-unsubscribe@xbean.org
-
-
- xbean users
- mailto:user-subscribe@xbean.org
- mailto:user-unsubscribe@xbean.org
-
-
- xbean source control messages
- mailto:scm-subscribe@xbean.org
- mailto:scm-unsubscribe@xbean.org
-
-
-
-
-
- David Blevins
- dblevins
- dblevins@visi.com
-
- Dude
-
-
-
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
-
- 1.4
- 1.4
-
-
-
-
-
-
-
diff --git a/classpath/project.properties b/classpath/project.properties
deleted file mode 100644
index 07280f09..00000000
--- a/classpath/project.properties
+++ /dev/null
@@ -1,34 +0,0 @@
-###
-# Copyright 2005 the original author or authors.
-#
-# Licensed 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.
-###
-maven.repo.remote=http://www.xbean.org/maven,http://www.openejb.org/maven,http://www.ibiblio.org/maven
-
-maven.compile.source=1.4
-maven.compile.target=1.4
-maven.compile.deprecation=true
-maven.compile.debug=true
-maven.compile.optimize=true
-
-maven.remote.group=xbean
-maven.username=${user.name}
-maven.repo.central=beaver.codehaus.org
-maven.repo.central.directory=/dist
-
-maven.javadoc.source=1.4
-maven.javadoc.links=http://java.sun.com/j2se/1.4.1/docs/api/
-maven.javadoc.additionalparam=-linksource
-
-maven.site.deploy.method=rsync
-maven.site.deploy.clean=true
diff --git a/classpath/project.xml b/classpath/project.xml
deleted file mode 100644
index 668108f0..00000000
--- a/classpath/project.xml
+++ /dev/null
@@ -1,86 +0,0 @@
-
-
-
-
- 3
-
- XBean :: ClassPath
- xbean-classpath
- xbean
- 2.0-SNAPSHOT
-
- XBean.org
- http://xbean.org
-
-
- org.xbean
-
- XBean: Classpath and classloader manipulation library
-
-
- Classpath and classloader manipulation library
-
-
- http://www.xbean.org/
-
- www.xbean.org
- /home/projects/xbean/public_html/maven
-
-
-
- xbean developers
- mailto:dev-subscribe@xbean.org
- mailto:dev-unsubscribe@xbean.org
-
-
- xbean users
- mailto:user-subscribe@xbean.org
- mailto:user-unsubscribe@xbean.org
-
-
- xbean source control messages
- mailto:scm-subscribe@xbean.org
- mailto:scm-unsubscribe@xbean.org
-
-
-
-
-
-
-
-
-
-
-
-
- src/java
- src/test
-
-
- **/*Test.java
-
-
-
-
-
- maven-jxr-plugin
- maven-javadoc-plugin
- maven-junit-report-plugin
-
-
-
-
diff --git a/classpath/src/test/org/xbean/classpath/SystemClassPathTest.java b/classpath/src/test/org/xbean/classpath/SystemClassPathTest.java
deleted file mode 100644
index 2082c584..00000000
--- a/classpath/src/test/org/xbean/classpath/SystemClassPathTest.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package org.xbean.classpath;
-
-/**
- * @version $Revision$ $Date$
- */
-
-import junit.framework.*;
-import org.xbean.classpath.SystemClassPath;
-
-import java.net.URL;
-
-public class SystemClassPathTest extends TestCase {
-
- public void testAddJarToPath() throws Exception {
- SystemClassPath systemClassPath = new SystemClassPath();
-
- ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
-
- try {
- systemClassLoader.loadClass("groovy.lang.GroovyShell");
- fail("Class already exists");
- } catch (ClassNotFoundException e) {
- // this should fail
- }
-
-
- URL groovyJar = new URL("http://www.ibiblio.org/maven/groovy/jars/groovy-SNAPSHOT.jar");
- systemClassPath.addJarToPath(groovyJar);
-
- try {
- systemClassLoader.loadClass("groovy.lang.GroovyShell");
- } catch (ClassNotFoundException e) {
- // this should fail pass
- fail("Class already exists");
- }
- }
-}
\ No newline at end of file
diff --git a/kernel/converting-gbean.apt b/kernel/converting-gbean.apt
deleted file mode 100644
index d93fc0d1..00000000
--- a/kernel/converting-gbean.apt
+++ /dev/null
@@ -1,54 +0,0 @@
- -----
- Converting xbean.org from m1 to m2
- -----
- Jason van Zyl
- -----
-
- What follows is a little description of the the process that I go through
- when converting an m1 project to an m2 project. In this example I'm going to
- use xbean.org, Dain Sundstrom's [yada yada yada] ...
-
- Usually when I start looking at converting a project from m1 to m2 I take a
- quick look at the directory structure because the standard layout we
- decided upon for m2 is slightly different from layout most common in m1
- and it's understandable that folks might not want to change their layout.
- I talked it over with Dain and because SVN makes it so easy to shuffle things
- around he decided it would be ok to use the m2 standard. Using the m2
- standards does make things easier because the standards for m2 are
- encapsulated in the Super POM that all m2 project inherit from. You can sort of
- think of the Super POM in Maven as the analog of <<>> in Java.
-
- - pitfalls in using the default structure
-
- - resources in the same directories as sources
-
- - some things we don't have in m2 right now are a clover plugin
-
- Updating the POM
-
- - change pomVersion to modelVersion and 3 to 4.0.0
-
- - the id element is deprecated an calculated in m2, so i changed the
- id element to artifactId
-
- - currentVersion to version
-
- - change the version from 1.0-SNAPSHOT to 1.0-alpha-1-SNAPSHOT
-
- - package element no longer exists, it will be the job of plugins
-
- - remove the siteAddress and siteDirectory elemement and replace it
- with the distributionManagement element
-
- - removed the entire build element because we are using m2 standards
- which are encapsulated in the Super POM.
-
- - remove the reports for now as we don't have many
-
-Annoyances
-
- The single biggest annoyance with converting from m1 to m2 is the fact that
- often times people have not made POMs for artifacts we have entered into the
- repository. With m2 the build will not work if artifacts do not have the
- accompanying POMs.
-
diff --git a/kernel/m2-readme.txt b/kernel/m2-readme.txt
deleted file mode 100644
index 3750464a..00000000
--- a/kernel/m2-readme.txt
+++ /dev/null
@@ -1,9 +0,0 @@
-Trying Maven 2.x
-----------------
-
-If you want to try m2 then simply run the mavenizer.sh script which
-will produce an "m2" directory. Move into the "m2" directory and
-
-m2 install
-
-That should compile, test, package and install
diff --git a/kernel/maven.xml b/kernel/maven.xml
deleted file mode 100644
index 0872ff3b..00000000
--- a/kernel/maven.xml
+++ /dev/null
@@ -1,34 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/kernel/mavenizer.sh b/kernel/mavenizer.sh
deleted file mode 100755
index 0c9696c3..00000000
--- a/kernel/mavenizer.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-DIR=m2
-
-rm -rf $DIR > /dev/null 2>&1
-mkdir $DIR
-
-cp -r src $DIR
-cp pom.xml $DIR
-
-(
- cd $DIR
-
- find . -name '\.svn' -exec rm -rf {} \;
-
- (
- cd src
-
- mkdir main ; mv java main
-
- ( cd test ; mkdir java ; mv org java )
-
- )
-
-)
diff --git a/kernel/pom.xml b/kernel/pom.xml
deleted file mode 100644
index d4735575..00000000
--- a/kernel/pom.xml
+++ /dev/null
@@ -1,113 +0,0 @@
-
-
-
-
- 4.0.0
-
- XBean :: Kernel
- xbean-kernel
- xbean
- 2.0-alpha-1-SNAPSHOT
-
- XBean
- http://xbean.org
-
-
-
-
- openejb
- OpenEJB Repo
- http://www.openejb.org/maven
- legacy
-
-
-
-
- XBean is a kernel architecture and server for ioc containers such as spring.
-
-
- http://www.xbean.org/
-
-
-
- repo1
- Maven Central Repository
- scp://repo1.maven.org/home/projects/maven/repository-staging/to-ibiblio/maven2
-
-
- website
- scp://minotaur.apache.org/www/maven.apache.org/maven2/
-
-
-
-
-
- xbean developers
- mailto:dev-subscribe@xbean.org
- mailto:dev-unsubscribe@xbean.org
-
-
- xbean users
- mailto:user-subscribe@xbean.org
- mailto:user-unsubscribe@xbean.org
-
-
- xbean source control messages
- mailto:scm-subscribe@xbean.org
- mailto:scm-unsubscribe@xbean.org
-
-
-
-
-
- Dain Sundstrom
- dain
- dain@iq80.com
-
- Despot
-
-
-
-
-
-
- backport-util-concurrent
- backport-util-concurrent
- 2.0_01_pd
-
-
- junit
- junit
- 3.8.1
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
-
- 1.4
- 1.4
-
-
-
-
-
-
-
diff --git a/kernel/project.properties b/kernel/project.properties
deleted file mode 100644
index 07280f09..00000000
--- a/kernel/project.properties
+++ /dev/null
@@ -1,34 +0,0 @@
-###
-# Copyright 2005 the original author or authors.
-#
-# Licensed 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.
-###
-maven.repo.remote=http://www.xbean.org/maven,http://www.openejb.org/maven,http://www.ibiblio.org/maven
-
-maven.compile.source=1.4
-maven.compile.target=1.4
-maven.compile.deprecation=true
-maven.compile.debug=true
-maven.compile.optimize=true
-
-maven.remote.group=xbean
-maven.username=${user.name}
-maven.repo.central=beaver.codehaus.org
-maven.repo.central.directory=/dist
-
-maven.javadoc.source=1.4
-maven.javadoc.links=http://java.sun.com/j2se/1.4.1/docs/api/
-maven.javadoc.additionalparam=-linksource
-
-maven.site.deploy.method=rsync
-maven.site.deploy.clean=true
diff --git a/kernel/project.xml b/kernel/project.xml
deleted file mode 100644
index 7be8e828..00000000
--- a/kernel/project.xml
+++ /dev/null
@@ -1,98 +0,0 @@
-
-
-
-
- 3
-
- XBean :: Kernel
- xbean-kernel
- xbean
- 2.0-SNAPSHOT
-
- XBean.org
- http://xbean.org
-
-
- org.xbean
-
- XBean: kernel for containers
-
-
- XBean is a kernel architecture and server for ioc containers such as spring.
-
-
- http://www.xbean.org/
-
- www.xbean.org
- /home/projects/xbean/public_html/maven
-
-
-
- xbean developers
- mailto:dev-subscribe@xbean.org
- mailto:dev-unsubscribe@xbean.org
-
-
- xbean users
- mailto:user-subscribe@xbean.org
- mailto:user-unsubscribe@xbean.org
-
-
- xbean source control messages
- mailto:scm-subscribe@xbean.org
- mailto:scm-unsubscribe@xbean.org
-
-
-
-
-
- backport-util-concurrent
- backport-util-concurrent
- 2.0_01_pd
-
-
-
-
- src/java
- src/test
-
-
- **/*Test.java
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- maven-jxr-plugin
- maven-javadoc-plugin
- maven-junit-report-plugin
-
-
-
-
-
-
-
diff --git a/kernel/src/java/org/xbean/kernel/AbstractServiceFactory.java b/kernel/src/java/org/xbean/kernel/AbstractServiceFactory.java
deleted file mode 100644
index 19010589..00000000
--- a/kernel/src/java/org/xbean/kernel/AbstractServiceFactory.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-import java.util.Collections;
-import java.util.HashSet;
-
-/**
- * AbstractServiceFactory is an implementation of ServiceFactory that handles all of the mundane issues.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public abstract class AbstractServiceFactory implements ServiceFactory {
- private boolean enabled = true;
- private final Set startConditions = new HashSet();
- private final Set stopConditions = new HashSet();
-
- /**
- * {@inheritDoc}
- */
- public synchronized boolean isEnabled() {
- return enabled;
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void setEnabled(boolean enabled) {
- this.enabled = enabled;
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized Set getStartConditions() {
- return Collections.unmodifiableSet(new HashSet(startConditions));
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void addStartCondition(ServiceCondition startCondition) throws NullPointerException {
- if (startCondition == null) throw new NullPointerException("startCondition is null");
- startConditions.add(startCondition);
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void removeStartCondition(ServiceCondition startCondition) throws NullPointerException {
- if (startCondition == null) throw new NullPointerException("startCondition is null");
- startConditions.remove(startCondition);
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized Set getStopConditions() {
- return Collections.unmodifiableSet(new HashSet(stopConditions));
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void addStopCondition(ServiceCondition stopCondition) throws NullPointerException {
- if (stopCondition == null) throw new NullPointerException("stopCondition is null");
- stopConditions.add(stopCondition);
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void removeStopCondition(ServiceCondition stopCondition) throws NullPointerException {
- if (stopCondition == null) throw new NullPointerException("stopCondition is null");
- stopConditions.remove(stopCondition);
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ForcedStopException.java b/kernel/src/java/org/xbean/kernel/ForcedStopException.java
deleted file mode 100644
index 88fbda2c..00000000
--- a/kernel/src/java/org/xbean/kernel/ForcedStopException.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Collections;
-import java.util.Set;
-
-/**
- * Signafies that a StopStrategies would like the kernel to ignore any unsatified stop conditions and continue to
- * destroy the service.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ForcedStopException extends Exception {
- private final ServiceName serviceName;
- private final Set unsatisfiedConditions;
-
- /**
- * Creates a ForcedStopException for the specified service name.
- *
- * @param serviceName the name of the service that is to be forceably stopped
- * @param unsatisfiedConditions the unsatisfied conditions that will be ignored
- */
- public ForcedStopException(ServiceName serviceName, Set unsatisfiedConditions) {
- super("Forced stop and ignored unsatisfied conditons:" +
- " serviceName=" + serviceName +
- ", unsatisfiedConditions=" + unsatisfiedConditions);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (unsatisfiedConditions == null) throw new NullPointerException("unsatisfiedConditions is null");
- this.serviceName = serviceName;
- this.unsatisfiedConditions = Collections.unmodifiableSet(unsatisfiedConditions);
- }
-
- /**
- * Gets the name of the service that is to be forceably stopped.
- *
- * @return the service name
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the conditions that were unsatified when the exception was thrown.
- *
- * @return the unsatified conditions that were ignored
- */
- public Set getUnsatisfiedConditions() {
- return unsatisfiedConditions;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/IllegalServiceStateException.java b/kernel/src/java/org/xbean/kernel/IllegalServiceStateException.java
deleted file mode 100644
index 16107b62..00000000
--- a/kernel/src/java/org/xbean/kernel/IllegalServiceStateException.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Indicates an operation was called on a service in a state that does not allow that operation to be called.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class IllegalServiceStateException extends Exception {
- private final ServiceName serviceName;
-
- /**
- * Creates an IllegalServiceStateException.
- *
- * @param message information about why the service is in an illegal state
- * @param serviceName the name of the service
- */
- public IllegalServiceStateException(String message, ServiceName serviceName) {
- super(message + ": " + serviceName);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- this.serviceName = serviceName;
- }
-
- /**
- * Gets the name of the service that caused this exception.
- *
- * @return the service name
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/InvalidServiceTypeException.java b/kernel/src/java/org/xbean/kernel/InvalidServiceTypeException.java
deleted file mode 100644
index e82cf409..00000000
--- a/kernel/src/java/org/xbean/kernel/InvalidServiceTypeException.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Indicates that the service factory returned an object from the createService method that is not an instance of every
- * declared type.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class InvalidServiceTypeException extends Exception {
- private final ServiceName serviceName;
- private final Class expectedType;
- private final Class serviceType;
-
- /**
- * Creates an InvalidServiceType caused by the service with the specified name, which returned an object from the
- * createService method of the specified type that is not an instance of the expected type.
- * @param serviceName the name of the service that returned an object of the wrong type
- * @param expectedType the type that was expected
- * @param serviceType the actual type of the service returned from the factory
- */
- // todo add servicefacotory to the parameters
- public InvalidServiceTypeException(ServiceName serviceName, Class expectedType, Class serviceType) {
- super("Expected service type " + expectedType.getName() + ", but service factory created a " +
- serviceType.getName() + " for service " + serviceName);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (expectedType == null) throw new NullPointerException("expectedType is null");
- if (serviceType == null) throw new NullPointerException("serviceType is null");
- this.serviceName = serviceName;
- this.expectedType = expectedType;
- this.serviceType = serviceType;
- }
-
- /**
- * Gets the name of the service that returned an object of the wrong type.
- * @return the name of the service that returned an object of the wrong type
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the type that was expected.
- * @return the type that was expected
- */
- public Class getExpectedType() {
- return expectedType;
- }
-
- /**
- * Gets the actual type of the service returned from the factory.
- * @return the actual type of the service returned from the factory
- */
- public Class getServiceType() {
- return serviceType;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/Kernel.java b/kernel/src/java/org/xbean/kernel/Kernel.java
deleted file mode 100644
index 1a8999fe..00000000
--- a/kernel/src/java/org/xbean/kernel/Kernel.java
+++ /dev/null
@@ -1,369 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.List;
-
-/**
- * This iterface defines the API for managing and monitoring service life-cycle. A kernel can be constructed with the
- * following code:
- *
- * Services can be registered, unregistered, started and
- * stopped. The lifecycle model is loosly based on the J2ee Management Specification (JSR 77). All lifecycle
- * transitions are broadcasted via a ServiceMonitor.
- *
- * Each kernel must have a name that is unique with in the KernelFactory (there should only be one KernelFactory per
- * VM but class loader tricks can result in several KernelFactory)
- *
- * This class is loosely based on the J2ee management MEJB and JMX MBeanServer interfaces.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface Kernel {
- /**
- * Destroys this kernel. This method causes all services to be stopped and unregistered.
- */
- void destroy();
-
- /**
- * Waits for the kernel to be destroyed.
- */
- void waitForDestruction();
-
- /**
- * Gets the running status of the kernel. Services can not be registered or started on a stopped kernel.
- *
- * @return true if the kernel is running; false otherwise
- */
- boolean isRunning();
-
- /**
- * Gets the unique name of this kernel within the KernelFactory.
- *
- * @return the unique name of this kernel
- */
- String getKernelName();
-
- /**
- * Registers a service with this kernel. If the service is restartable, it will enter the server in the
- * STOPPED state. If a service is not restartable, the kernel will assure that all
- * dependencies are satisfied and service will enter the kernel in the RUNNING state. If a
- * dependency for a non-restartable service is not immediately satisfiable, this method will throw a
- * ServiceRegistrationException.
- *
- * @param serviceName the unique name of the service in the kernel
- * @param serviceFactory the factory used to create the service
- * @param classLoader the class loader to use for this service
- * @throws ServiceAlreadyExistsException if service is already registered with the specified name
- * @throws ServiceRegistrationException if the service is not restartable and an error occured while starting the service
- */
- void registerService(ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader) throws ServiceAlreadyExistsException, ServiceRegistrationException;
-
- /**
- * Unregisters a service from this kernel. The kernel will attempt to stop the service using the
- * SYNCHRONOUS stop strategy, but if it can not stop the service a
- * ServiceRegistrationException will be thrown containing an UnsatisfiedConditionsException.
- *
- * @param serviceName the unique name of the service in the kernel
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws ServiceRegistrationException if the service could not be stopped
- */
- void unregisterService(ServiceName serviceName) throws ServiceNotFoundException, ServiceRegistrationException;
-
- /**
- * Unregisters a service from this kernel. The kernel will attempt to stop the service using the specified stop
- * strategy, but if it can not stop the service a ServiceRegistrationException will be thrown containing
- * either an UnsatisfiedConditionsException or a IllegalServiceStateException.
- *
- * @param serviceName the unique name of the service in the kernel
- * @param stopStrategy the strategy that determines how unsatisfied conditions are handled
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws ServiceRegistrationException if the service could not be stopped
- */
- void unregisterService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, ServiceRegistrationException;
-
- /**
- * Determines if there is a service registered under the specified name.
- *
- * @param serviceName the unique name of the service
- * @return true if there is a service registered with the specified name; false otherwise
- */
- boolean isRegistered(ServiceName serviceName);
-
- /**
- * Gets the ServiceState of the specified service. If the service is not restartable, this method will
- * always return RUNNING.
- *
- * @param serviceName the unique name of the service in the kernel
- * @return the curren ServiceState of the service
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- ServiceState getServiceState(ServiceName serviceName) throws ServiceNotFoundException;
-
- /**
- * Gets the time the specified service entered the RUNNING state since the epoch
- * (January 1, 1970, 00:00:00) in milliseconds. If the service is in the STOPPED or
- * STARTING states, this method will return 0.
- *
- * @param serviceName the unique name of the service in the kernel
- * @return the time the service started in milliseconds since January 1, 1970, 00:00:00
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- long getServiceStartTime(ServiceName serviceName) throws ServiceNotFoundException;
-
- /**
- * Immediately starts the service using the SYNCHRONOUS start strategy. Any exception throw
- * from service constuction is throw directly from this method. If a start condition can not be immediately
- * satisfied, a UnsatisfiedConditionsException will be thrown. If a service already in the
- * RUNNING state, or is not restartable, this method is a noop. If the service
- * is in the STOPPING state an IllegalServiceStateException will be thrown. If the
- * service is disabled, this method will throw an IllegalServiceStateException.
- *
- * This method has no effect on as service that is not restartable.
- *
- * @param serviceName the unique name of the service to start
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws IllegalServiceStateException if the service is restartable and is in the STOPPING state or if the
- * service is disabled
- * @throws UnsatisfiedConditionsException if some of the start conditions can not be immediately satisfied
- * @throws Exception if service construction threw an Exception
- */
- void startService(ServiceName serviceName) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception;
-
- /**
- * Immediately starts the service using the specified start strategy.
- *
- * The start strategy determines how any exception thrown from service constuction is handled including throwing
- * the exception directly from this method.
- *
- * The start strategy determines what to do if a start condition can not be immediately satisfied. Possibilities
- * include throwing an UnsatisfiedConditionsException, blocking, leaving the service in the
- * RUNNING state, or unregistering the service.
- *
- * If a service already in the RUNNING state, or is not restartable, this method is a noop.
- * If the service is in the STOPPING state an IllegalServiceStateException will be
- * thrown. If the service is disabled, this method will throw an IllegalServiceStateException.
- *
- * This method has no effect on as service that is not restartable.
- *
- * @param serviceName the unique name of the service to start
- * @param startStrategy the strategy that determines how unsatisfied conditions and construction exceptions are handled
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws IllegalServiceStateException if the service is restartable and is in the STOPPING state or if the
- * service is disabled
- * @throws UnsatisfiedConditionsException if some of the start conditions can not be immediately satisfied
- * @throws Exception if service construction threw an Exception
- */
- void startService(ServiceName serviceName, StartStrategy startStrategy) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception;
-
- /**
- * Immediately starts the service, and if the start ultimately completes successfully, all services owned by the
- * specified service, all services that are owned by those services, and so on, will be started using the
- * startServiceRecursive(ServiceName) method.
- *
- * @param serviceName the unique name of the service to start recursively
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws IllegalServiceStateException if the service is restartable and is in the STOPPING state or if the
- * service is disabled
- * @throws UnsatisfiedConditionsException if some of the start conditions can not be immediately satisfied
- * @throws Exception if service construction threw an Exception
- */
- void startServiceRecursive(ServiceName serviceName) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception;
-
- /**
- * Immediately starts the service, and if the start ultimately completes successfully, all services owned by the
- * specified service, all services that are owned by those services, and so on, will be started using the
- * startServiceRecursive(ServiceName, StartStrategy) method.
- *
- * @param serviceName the unique name of the service to start recursively
- * @param startStrategy the strategy that determines how unsatisfied conditions and construction exceptions are handled
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws IllegalServiceStateException if the service is restartable and is in the STOPPING state or if the
- * service is disabled
- * @throws UnsatisfiedConditionsException if some of the start conditions can not be immediately satisfied
- * @throws Exception if service construction threw an Exception
- */
- void startServiceRecursive(ServiceName serviceName, StartStrategy startStrategy) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception;
-
- /**
- * Immediately stops the service using the SYNCHRONOUS stop strategy. If a stop condition can
- * not be immediately satisfied, an UnsatisfiedConditionsException will be thrown. If a service already in
- * the STOPPED state, this method is a noop.
- *
- * If the service is not restartable, this method only attempts to satify the stop conditions. This is useful for
- * stopping all dependent services of a non-restartable service before unregistering the service.
- *
- * @param serviceName the unique name of the service to stop
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- void stopService(ServiceName serviceName) throws ServiceNotFoundException, UnsatisfiedConditionsException;
-
- /**
- * Immediately stops the service using the specified stop strategy. If a stop condition can not be immediately
- * satisfied, an UnsatisfiedConditionsException will be thrown. If a service already in the
- * STOPPED state, this method is a noop.
- *
- * If the service is not restartable, this method only attempts to satify the stop conditions. This is useful for
- * stopping all dependent services of a non-restartable service before unregistering the service.
- *
- * @param serviceName the unique name of the service to stop
- * @param stopStrategy the strategy that determines how unsatisfied conditions are handled
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- void stopService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, UnsatisfiedConditionsException;
-
- /**
- * Determines if the service can be instantiated in a kernel. A disabled restartable service can not be
- * started. This method is equivalent to:
- *
- *
- *
- * @param serviceName the unique name of the service
- * @return true if the service factory is enabled; false otherwise
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- boolean isServiceEnabled(ServiceName serviceName) throws ServiceNotFoundException;
-
- /**
- * Sets the enabled status of a service. A disabled restartable service can not be started. This state has
- * no effect on a service that is already started, but if a running service is disabled, it can not be restarted.
- * This method is equivalent to:
- *
- *
- *
- * @param serviceName the unique name of the service
- * @param enabled the new enabled state of this factory
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- void setServiceEnabled(ServiceName serviceName, boolean enabled) throws ServiceNotFoundException;
-
- /**
- * Gets the service registered under the specified name. If the service is not in the RUNNING,
- * or STARTING state this method will throw an IllegalArgumentException.
- *
- * @param serviceName the unique name of the service
- * @return the service associated with the specified name
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws IllegalArgumentException if the service is not in the RUNNING, or STARTING state
- */
- Object getService(ServiceName serviceName) throws ServiceNotFoundException, IllegalArgumentException;
-
- /**
- * Gets the first running service registered with the kernel that is an instance of the specified type. If no
- * running services are instances of the specified type, null is returned.
- *
- * @param type the of the desired service
- * @return the first registered service that is an instance of the specified type and is running
- */
- Object getService(Class type);
-
- /**
- * Gets the all of running service registered with the kernel that are an instances of the specified type. If no
- * running services are instances of the specified type, an empty list is returned
- *
- * @param type the of the desired service
- * @return the registered services that are instances of the specified type and are running
- */
- List getServices(Class type);
-
- /**
- * Gets the service factory registered under the specified name.
- *
- * @param serviceName the unique name of the service
- * @return the service factory associated with the specified name
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- ServiceFactory getServiceFactory(ServiceName serviceName) throws ServiceNotFoundException;
-
- /**
- * Gets the first service factory registered with the kernel that creates an instance of the specified type.
- * If no service factories create an instance of the specified type, null is returned.
- *
- * @param type the of the desired service
- * @return the first service factory registered with the kernel that creates an instance of the specified type
- */
- ServiceFactory getServiceFactory(Class type);
-
- /**
- * Gets the all of the service factories registered with the kernel that create an instances of the specified type.
- * If no service factories create an instance of the specified type, an empty list is returned.
- *
- * @param type the of the desired service
- * @return the registered services that are instances of the specified type and are running
- */
- List getServiceFactories(Class type);
-
- /**
- * Gets the class loader associated with the specifed service.
- *
- * @param serviceName the unique name of the service
- * @return the class loader associated with the specified name
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- ClassLoader getClassLoaderFor(ServiceName serviceName) throws ServiceNotFoundException;
-
- /**
- * Adds a kernel monitor.
- *
- * @param kernelMonitor the kernel monitor to add
- */
- void addKernelMonitor(KernelMonitor kernelMonitor);
-
- /**
- * Removes a kernel monitor.
- *
- * @param kernelMonitor the kernel monitor to remove
- */
- void removeKernelMonitor(KernelMonitor kernelMonitor);
-
- /**
- * Adds a service monitor for all services registered with the kernel. This method is equivalent to:
- *
- * addServiceMonitor(serviceMonitor, null);
- *
- *
- * Note: the order in which service monitors are notified is not specified.
- *
- * @param serviceMonitor the service monitor to add
- */
- void addServiceMonitor(ServiceMonitor serviceMonitor);
-
- /**
- * Adds a service monitor for a specific service.
- *
- * Note: the order in which service monitors are notified is not specified.
- *
- * @param serviceMonitor the service monitor to add
- * @param serviceName the unique name of the service to monitor or null to monitor all services
- */
- void addServiceMonitor(ServiceMonitor serviceMonitor, ServiceName serviceName);
-
- /**
- * Removes a service monitor.
- *
- * @param serviceMonitor the service monitor to remove
- */
- void removeServiceMonitor(ServiceMonitor serviceMonitor);
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelAlreadyExistsException.java b/kernel/src/java/org/xbean/kernel/KernelAlreadyExistsException.java
deleted file mode 100644
index 1c74fa31..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelAlreadyExistsException.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Indicates that a kernel is already registerd with the KernelFactory under the specified name.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelAlreadyExistsException extends RuntimeException {
- private final String name;
-
- /**
- * Creates a KernelAlreadyExistsException using the specified name.
- *
- * @param name the name of the kernel that was alredy registered
- */
- public KernelAlreadyExistsException(String name) {
- super("A kernel is already registered with the name " + name);
- if (name == null) throw new NullPointerException("name is null");
- this.name = name;
- }
-
- /**
- * Gets the name of the kernel that already existed.
- *
- * @return the name of the kernel that already existed.
- */
- public String getName() {
- return name;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelErrorsError.java b/kernel/src/java/org/xbean/kernel/KernelErrorsError.java
deleted file mode 100644
index 86163f9c..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelErrorsError.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.io.PrintStream;
-import java.io.PrintWriter;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ListIterator;
-
-/**
- * Groups a collection of errors from a set of work so they maybe be thrown together from the kernel. This is used
- * when the kernel does aggregate work on somthing that shouldn't fail such as when notifying kernel monitors or
- * destroying the kernel. This allows the kernel to preform all required work and then throw any errors as a single
- * exception object.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelErrorsError extends Error {
- private final List errors;
-
- /**
- * Creates an Errors error containing the list of errors.
- *
- * @param errors the errors
- */
- public KernelErrorsError(List errors) {
- if (errors == null) throw new NullPointerException("errors is null");
- if (errors.isEmpty()) throw new IllegalArgumentException("errors is empty");
- for (ListIterator iterator = errors.listIterator(); iterator.hasNext();) {
- Object error = iterator.next();
- if (error == null) {
- throw new IllegalArgumentException("Errors element " + iterator.previousIndex() + " is null");
- }
- if (!(error instanceof Error)) {
- throw new IllegalArgumentException("Errors element " + iterator.previousIndex() +
- " is not an instance of java.lang.Error " + error.getClass() + ": " + error);
- }
- }
-
- this.errors = Collections.unmodifiableList(errors);
- }
-
- /**
- * Gets the errors that casued this error.
- *
- * @return the errors that casued this error
- */
- public List getErrors() {
- return errors;
- }
-
- public String getMessage() {
- StringBuffer message = new StringBuffer();
- message.append(errors.size() + " Error(s) occured [");
- for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
- Error error = (Error) iterator.next();
- message.append('\"').append(error.getMessage()).append('\"');
- if (iterator.hasNext()) {
- message.append(", ");
- }
- }
- return message.append("]").toString();
- }
-
- public String getLocalizedMessage() {
- StringBuffer message = new StringBuffer();
- message.append(errors.size() + " Error(s) occured [");
- for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
- Error error = (Error) iterator.next();
- message.append('\"').append(error.getLocalizedMessage()).append('\"');
- if (iterator.hasNext()) {
- message.append(", ");
- }
- }
- return message.append("]").toString();
- }
-
- public void printStackTrace(PrintStream stream) {
- synchronized (stream) {
- stream.println(this);
- for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
- Error error = (Error) iterator.next();
- error.printStackTrace(stream);
- }
- }
- }
-
- public void printStackTrace(PrintWriter writer) {
- synchronized (writer) {
- writer.println(this);
- for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
- Error error = (Error) iterator.next();
- error.printStackTrace(writer);
- }
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelFactory.java b/kernel/src/java/org/xbean/kernel/KernelFactory.java
deleted file mode 100644
index b8f04fa0..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelFactory.java
+++ /dev/null
@@ -1,193 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.HashMap;
-import java.util.Map;
-
-import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
-import org.xbean.kernel.standard.StandardKernelFactory;
-
-/**
- * The Kernel factory is used to construct and locate Kernels. This class is loosly based on the SAXParserFactory and
- * the JMX MBeanServerFactory. To constuct a kernel use the following:
- *
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public abstract class KernelFactory {
- /**
- * The name of the system property and META-INF/services used to locate the kernel factory class.
- */
- public static final String KERNEL_FACTORY_KEY = KernelFactory.class.getName();
-
- private static final ConcurrentHashMap kernels = new ConcurrentHashMap(1);
-
- /**
- * Gets the kernel registered under the specified name. If no kernel is registered with the specified name, null
- * is returned.
- *
- * @param name the name of the kernel to return
- * @return the kernel or null if no kernel is registered under the specified name
- */
- public static Kernel getKernel(String name) {
- if (name == null) throw new NullPointerException("name is null");
- return (Kernel) kernels.get(name);
- }
-
- /**
- * Gets a map of the existing kernels by kernel name.
- *
- * @return the existing kernels by kernel name.
- */
- public static Map getKernels() {
- return new HashMap(kernels);
- }
-
- /**
- * Creates a kernel with the specified name. This method will attempt to locate a KernelFactory implementation
- * using the following procedure
- *
- * The org.xbean.kernel.KernelFactory system property.
- *
- * Use the Jar Service Specification
- * This method will attempt to get the factory name from the file
- * META-INF/services/org.xbean.kernel.KernelFactory loaded using the thread context class loader.
- *
- *
- * The StandardKernel implementation.
- *
- *
- * The factory class is loaded and constucted using the thread context class loader, if present, or the class
- * loader of this class.
- *
- * @return the kernel factory implementation
- * @throws KernelFactoryError if the specified kernel factory can not be created
- */
- public static KernelFactory newInstance() throws KernelFactoryError {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- if (classLoader == null) {
- classLoader = KernelFactory.class.getClassLoader();
- }
-
- // System property
- try {
- String kernelFactoryName = System.getProperty(KERNEL_FACTORY_KEY);
- if (kernelFactoryName != null) {
- return createKernelFactory(kernelFactoryName, classLoader);
- }
- } catch (SecurityException se) {
- }
-
- // Jar Service Specification - http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
- String serviceId = "META-INF/services/" + KERNEL_FACTORY_KEY;
- InputStream inputStream = null;
- try {
- inputStream = classLoader.getResourceAsStream(serviceId);
- if (inputStream != null) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
- String kernelFactoryName = reader.readLine();
- reader.close();
-
- if (kernelFactoryName != null && kernelFactoryName.length() > 0) {
- return createKernelFactory(kernelFactoryName, classLoader);
- }
- }
- } catch (Exception ignored) {
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException ignored) {
- }
- inputStream = null;
- }
- }
-
- // Default is the standard kernel
- return new StandardKernelFactory();
- }
-
- /**
- * Removes the kernel instance from the internal kernel registry. This method should only be called by the kernel
- * instance itself during destruction.
- * @param kernel the kernel to destroy
- * @throws KernelFactoryError if the kernel is still running
- */
- public static void destroyInstance(Kernel kernel) throws KernelFactoryError {
- if (kernel.isRunning()) {
- throw new KernelFactoryError("Kernel is running: name" + kernel.getKernelName());
- }
-
- kernels.remove(kernel.getKernelName(), kernel);
- }
-
- private static KernelFactory createKernelFactory(String className, ClassLoader classLoader) throws KernelFactoryError {
- try {
- return (KernelFactory) classLoader.loadClass(className).newInstance();
- } catch (ClassCastException e) {
- throw new KernelFactoryError("Kernel factory class does not implement KernelFactory: " + className);
- } catch (ClassNotFoundException e) {
- throw new KernelFactoryError("Kernel factory class not found: " + className);
- } catch (Exception e) {
- throw new KernelFactoryError("Unable to instantiate kernel factory class: " + className, e);
- }
- }
-
- /**
- * Creates a new kernel instance and registers it with the static KernelFactory registry. This allows the kernel
- * to be retrieved from the {@link KernelFactory#getKernel(String)} method.
- *
- * @param name the name of the kernel to create
- * @return the new kernel
- * @throws KernelAlreadyExistsException is a kernel already exists with the specified name
- */
- public final Kernel createKernel(String name) throws KernelAlreadyExistsException {
- if (name == null) throw new NullPointerException("name is null");
-
- // quick check to see if a kernel already registerd wit the name
- if (kernels.containsKey(name)) {
- throw new KernelAlreadyExistsException(name);
- }
-
- // create the kernel -- this may be an unnecessary construction, but it shouldn't be a big deal
- Kernel kernel = createKernelInternal(name);
-
- // register the kernel, checking if someone snuck in an registered a kernel while we were creating ours
- if (kernels.putIfAbsent(name, kernel) != null) {
- throw new KernelAlreadyExistsException(name);
- }
-
- return kernel;
- }
-
- /**
- * Creates the actual kernel instance which will be registerd in the KernelFactory.
- *
- * @param name the kernel name
- * @return a new kernel instance
- */
- protected abstract Kernel createKernelInternal(String name);
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelFactoryError.java b/kernel/src/java/org/xbean/kernel/KernelFactoryError.java
deleted file mode 100644
index d986f50e..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelFactoryError.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * A problem occured while creating or using the kernel factory. This error indicates that the kernel factory is
- * misconfigured or there is a programming error in the use of the kernel factory.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelFactoryError extends Error {
- /**
- * Creates a KernelFactoryError using the specified message.
- *
- * @param message information about the cause of this error
- */
- public KernelFactoryError(String message) {
- super(message);
- }
-
- /**
- * Creates a KernelFactoryError using the specified message and cause.
- *
- * @param message information about the cause of this error
- * @param cause the cause of this error
- */
- public KernelFactoryError(String message, Throwable cause) {
- super(message, cause);
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelMonitor.java b/kernel/src/java/org/xbean/kernel/KernelMonitor.java
deleted file mode 100644
index 9ecc6eed..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelMonitor.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * This interface defines the interface used to monitor kernel events. A KernelMonitor can be registered with the
- * kernel using the {@link Kernel#addKernelMonitor(KernelMonitor)} method.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface KernelMonitor {
- /**
- * An error occured with notifiying a service monitor.
- *
- * @param serviceMonitor the monitor that threw the exception
- * @param serviceEvent the event that was being processed
- * @param throwable the exception that was thrown
- */
- void serviceNotificationError(ServiceMonitor serviceMonitor, ServiceEvent serviceEvent, Throwable throwable);
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelOperationInterruptedException.java b/kernel/src/java/org/xbean/kernel/KernelOperationInterruptedException.java
deleted file mode 100644
index f8a12e04..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelOperationInterruptedException.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Signifies that a thread carrying out a kernel operation was interrupted. The kernel will always leave the
- * system in a stable state before returning to the caller.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelOperationInterruptedException extends RuntimeException {
- private final ServiceName serviceName;
- private final String operationName;
-
- /**
- * Created a KernelOperationInterruptedException for the specified operation on the specified service.
- *
- * @param cause the {@link InterruptedException} that casused the kernel operation to be interrupted
- * @param serviceName the name of the service for which the operation was interrupted
- * @param operationName the name of the operation that was interrupted
- */
- public KernelOperationInterruptedException(InterruptedException cause, ServiceName serviceName, String operationName) {
- super(cause);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (operationName == null) throw new NullPointerException("operationName is null");
- this.serviceName = serviceName;
- this.operationName = operationName;
- }
-
- /**
- * Created a KernelOperationInterruptedException with a custom message.
- *
- * @param message a custom message for this exception
- * @param cause the {@link InterruptedException} that casused the kernel operation to be interrupted
- * @param serviceName the name of the service for which the operation was interrupted
- * @param operationName the name of the operation that was interrupted
- */
- public KernelOperationInterruptedException(String message, InterruptedException cause, ServiceName serviceName, String operationName) {
- super(message, cause);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (operationName == null) throw new NullPointerException("operationName is null");
- this.serviceName = serviceName;
- this.operationName = operationName;
- }
-
- /**
- * Gets the name of the service for which the operation was interrupted.
- *
- * @return the name of the service for which the operation was interrupted
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the name of the operation that was interrupted.
- *
- * @return the name of the operation that was interrupted
- */
- public String getOperationName() {
- return operationName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/KernelOperationTimoutException.java b/kernel/src/java/org/xbean/kernel/KernelOperationTimoutException.java
deleted file mode 100644
index 3cadfce6..00000000
--- a/kernel/src/java/org/xbean/kernel/KernelOperationTimoutException.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Signifies that a kernel operation timed out before it could be completed. The kernel will always leave the
- * system in a stable state before returning to the caller.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelOperationTimoutException extends RuntimeException {
- private final ServiceName serviceName;
- private final String operationName;
-
- /**
- * Created a KernelOperationTimoutException for the specified operation on the specified service.
- *
- * @param serviceName the name of the service for which the operation timed out
- * @param operationName the name of the operation that timed out
- */
- public KernelOperationTimoutException(ServiceName serviceName, String operationName) {
- super("Kernel operation timed out: serviceName=" + serviceName + ", operationName=" + operationName);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (operationName == null) throw new NullPointerException("operationName is null");
- this.serviceName = serviceName;
- this.operationName = operationName;
- }
-
- /**
- * Created a KernelOperationTimoutException using the specified custom message.
- *
- * @param message a custom message for this exception
- * @param serviceName the name of the service for which the operation timed out
- * @param operationName the name of the operation that timed out
- */
- public KernelOperationTimoutException(String message, ServiceName serviceName, String operationName) {
- super(message);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (operationName == null) throw new NullPointerException("operationName is null");
- this.serviceName = serviceName;
- this.operationName = operationName;
- }
-
- /**
- * Gets the name of the service for which the operation timed out.
- *
- * @return the name of the service for which the operation timed out
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the name of the operation that timed out.
- *
- * @return the name of the operation that timed out
- */
- public String getOperationName() {
- return operationName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/NullServiceMonitor.java b/kernel/src/java/org/xbean/kernel/NullServiceMonitor.java
deleted file mode 100644
index ae721372..00000000
--- a/kernel/src/java/org/xbean/kernel/NullServiceMonitor.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * The NullServiceMonitor is a simple implementation of ServiceMonitor containing a noop implementaion of
- * each callback.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class NullServiceMonitor implements ServiceMonitor {
- /**
- * {@inheritDoc}
- */
- public void serviceRegistered(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStarting(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceWaitingToStart(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStartError(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceRunning(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopping(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceWaitingToStop(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopError(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopped(ServiceEvent serviceEvent) {
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceUnregistered(ServiceEvent serviceEvent) {
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/RunningServiceCondition.java b/kernel/src/java/org/xbean/kernel/RunningServiceCondition.java
deleted file mode 100644
index 764d0d92..00000000
--- a/kernel/src/java/org/xbean/kernel/RunningServiceCondition.java
+++ /dev/null
@@ -1,143 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * This condition that requires another service be in the RUNNING state to be satisfied.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class RunningServiceCondition implements ServiceCondition {
- private final ServiceName dependency;
- private final boolean ownedRelationship;
- private final boolean stopOnServiceShutdown;
- private final DependencyServiceMonitor serviceMonitor = new DependencyServiceMonitor();
-
- private ServiceConditionContext context;
- private boolean satisfied = true;
- private StoppedServiceCondition stoppedServiceCondition;
-
- /**
- * Creates a condition that requires the specified service be in the RUNNING state to be satisfied.
- *
- * @param dependency the service that must be running
- * @param ownedRelationship if true the condition will register the relationship
- * @param stopOnServiceShutdown if the our service should be stopped when the specified service shutsdown
- */
- public RunningServiceCondition(ServiceName dependency, boolean ownedRelationship, boolean stopOnServiceShutdown) {
- if (dependency == null) throw new NullPointerException("dependency is null");
- this.dependency = dependency;
- this.ownedRelationship = ownedRelationship;
- this.stopOnServiceShutdown = stopOnServiceShutdown;
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void initialize(ServiceConditionContext context) {
- if (context == null) throw new NullPointerException("context is null");
-
- // if we have no been destroyed, destroy not
- if (this.context != null) {
- destroy();
- }
-
- this.context = context;
-
- satisfied = false;
- context.getKernel().addServiceMonitor(serviceMonitor, dependency);
- if (ownedRelationship) {
- // todo register owned relationship
- }
-
- if (stopOnServiceShutdown) {
- stoppedServiceCondition = new StoppedServiceCondition(context.getServiceName());
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized boolean isSatisfied() {
- if (context == null) {
- // we are not initialized so default to true
- return true;
- }
-
- if (!satisfied) {
- try {
- // grab a synchronized lock on the service factory to assure that the state doesn't change while
- // adding the dependency.... the kernel will grab the same lock when getting the stop dependencies
- ServiceFactory serviceFactory = context.getKernel().getServiceFactory(dependency);
- synchronized (serviceFactory) {
- if (context.getKernel().getService(dependency) == ServiceState.RUNNING) {
- if (stopOnServiceShutdown) {
- serviceFactory.addStopCondition(stoppedServiceCondition);
- }
- satisfied = true;
- context.getKernel().removeServiceMonitor(serviceMonitor);
- }
- }
- } catch (ServiceNotFoundException ignored) {
- // service hasn't registered yet
- }
- }
- return satisfied;
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void destroy() {
- if (context == null) {
- // we are already destroyed
- return;
- }
-
- context.getKernel().removeServiceMonitor(serviceMonitor);
- context = null;
- satisfied = true;
- if (ownedRelationship) {
- // todo unregister owned relationship
- }
- if (stopOnServiceShutdown) {
- stoppedServiceCondition.destroy();
- stoppedServiceCondition = null;
- }
- }
-
- private class DependencyServiceMonitor extends NullServiceMonitor {
- public void serviceRunning(ServiceEvent serviceEvent) {
- synchronized (RunningServiceCondition.this) {
- if (context != null) {
- // we aren't running anymore
- return;
- }
-
- if (!satisfied) {
- return;
- }
-
- if (isSatisfied()) {
- context.setSatisfied();
- }
- }
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceAlreadyExistsException.java b/kernel/src/java/org/xbean/kernel/ServiceAlreadyExistsException.java
deleted file mode 100644
index f0e84dea..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceAlreadyExistsException.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Signifies that an attempt was made to register a service using a name that already has a service registered.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceAlreadyExistsException extends Exception {
- private final ServiceName serviceName;
-
- /**
- * Creates a ServiceAlreadyExistsException for the specified service name.
- *
- * @param serviceName the name of the service that already exists
- */
- public ServiceAlreadyExistsException(ServiceName serviceName) {
- if (serviceName == null) throw new NullPointerException("name is null");
- this.serviceName = serviceName;
- }
-
- /**
- * Gets the name of the service that caused this exception.
- *
- * @return the service name
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceCondition.java b/kernel/src/java/org/xbean/kernel/ServiceCondition.java
deleted file mode 100644
index 53841b92..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceCondition.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * A ServiceContion represents a prerequsite for a service to start or stop. A condition can be added to a service with
- * the {@link ServiceFactory#addStartCondition(ServiceCondition)} or
- * {@link ServiceFactory#addStopCondition(ServiceCondition)} methods.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface ServiceCondition {
- /**
- * Initializes the condition. The conition is now allowed reserve unique resources and start threads.
- * mehtod should never block the thread nor should it throw any exceptions.
- *
- * Note: this method is called from within a critical lock within the kernel, so do not block the thread or
- * call back into the kernel. This method should never throw an exception.
- *
- * @param context context information for this condition
- */
- void initialize(ServiceConditionContext context);
-
- /**
- * Gets statisfied state of this conditon. Once a condition returns true from this method it is assumed to be satisfied until
- * destroyed and reinitialized.
- *
- * Note: this method is called from within a critical lock within the kernel, so do not block the thread or
- * call back into the kernel. This method should never throw an exception.
- *
- * @return true if this condition is satisfied; false otherwise
- */
- boolean isSatisfied();
-
- /**
- * Destroys the condition. The condition must release all resources and stop any started threads.
- *
- * Note: this method is called from within a critical lock within the kernel, so do not block the thread or
- * call back into the kernel. This method should never throw an exception.
- */
- void destroy();
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceConditionContext.java b/kernel/src/java/org/xbean/kernel/ServiceConditionContext.java
deleted file mode 100644
index f53eaa79..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceConditionContext.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * The ServiceConditionContext contains context information available to a service condition and a method to notify
- * the kernel if a service condition is satisified asynchronously.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface ServiceConditionContext {
- /**
- * Gets the kernel in which the service is registered.
- *
- * @return the kernel in which the service is registered
- */
- Kernel getKernel();
-
- /**
- * Gets the unique name of the service.
- *
- * @return the unique name of the service
- */
- ServiceName getServiceName();
-
- /**
- * Gets the class loader for the service.
- *
- * @return the class loader for the service
- */
- ClassLoader getClassLoader();
-
- /**
- * Used to notify the container that the condition has been satisfied asychronously.
- */
- void setSatisfied();
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceContext.java b/kernel/src/java/org/xbean/kernel/ServiceContext.java
deleted file mode 100644
index 1a3c5e70..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceContext.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * This class contains context information available to a service factory during service construction and destruction.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface ServiceContext {
- /**
- * Gets the kernel in which this service is registered.
- *
- * @return the kernel in which this service is registered
- */
- Kernel getKernel();
-
- /**
- * Gets the unique name of the service in the kernel.
- *
- * @return the unique name of this service in the kernel
- */
- ServiceName getServiceName();
-
- /**
- * Gets the class loader for this service.
- *
- * @return the class loader for this service
- */
- ClassLoader getClassLoader();
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceContextThreadLocal.java b/kernel/src/java/org/xbean/kernel/ServiceContextThreadLocal.java
deleted file mode 100644
index 4ba06ed7..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceContextThreadLocal.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * ServiceContextThreadLocal carries the ServiceContext on the Thread during service construction and destruction.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public final class ServiceContextThreadLocal {
- private ServiceContextThreadLocal() {
- }
-
- private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();
-
- /**
- * Gets the ServiceContext associated with the current thread.
- * @return the ServiceContext associated with the current thread
- */
- public static ServiceContext get() {
- return (ServiceContext) THREAD_LOCAL.get();
- }
-
- /**
- * Assocates the specified ServiceContext with the current thread.
- * @param serviceContext the service context to associate with the current thread
- */
- public static void set(ServiceContext serviceContext) {
- THREAD_LOCAL.set(serviceContext);
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceEvent.java b/kernel/src/java/org/xbean/kernel/ServiceEvent.java
deleted file mode 100644
index e3e68783..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceEvent.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-
-/**
- * This class holds information about a service event.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceEvent {
- private final long eventId;
- private final Kernel kernel;
- private final ServiceName serviceName;
- private final ServiceFactory serviceFactory;
- private final ClassLoader classLoader;
- private final Object service;
- private final Throwable cause;
- private final Set unsatisfiedConditions;
-
- /**
- * Creates a service event.
- *
- * @param eventId the sequence number for this event
- * @param kernel the kernel in which the service is registered
- * @param serviceName the name of the service
- * @param serviceFactory the factory for the service
- * @param classLoader the class loader for the service
- * @param service the service instance if it exists
- * @param cause the exception that caused the event if this is an exception event
- * @param unsatisfiedConditions the unsatified conditions that caused the event if this is a waiting event
- */
- public ServiceEvent(long eventId, Kernel kernel, ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader, Object service, Throwable cause, Set unsatisfiedConditions) {
- if (kernel == null) throw new NullPointerException("kernel is null");
- if (serviceName == null) throw new NullPointerException("name is null");
- if (serviceFactory == null) throw new NullPointerException("serviceFactory is null");
- if (classLoader == null) throw new NullPointerException("classLoader is null");
- if (unsatisfiedConditions != null && cause != null) throw new IllegalArgumentException("Either unsatisfiedConditions or cause must be null");
- if (cause != null && service != null) throw new IllegalArgumentException("A ServiceEvent can not carry both a cause and a service");
- this.eventId = eventId;
- this.kernel = kernel;
- this.serviceName = serviceName;
- this.serviceFactory = serviceFactory;
- this.classLoader = classLoader;
- this.service = service;
- this.cause = cause;
- this.unsatisfiedConditions = unsatisfiedConditions;
- }
-
- /**
- * Gets the sequence number for this event. A service gaurentees that events will occur in increasing order with out
- * skipping any numbers.
- *
- * @return the sequence number for this event
- */
- public long getEventId() {
- return eventId;
- }
-
- /**
- * Gets the kernel in which the service is registered.
- *
- * @return the kernel in which the servce is registerd
- */
- public Kernel getKernel() {
- return kernel;
- }
-
- /**
- * Gets the name of the service.
- *
- * @return the name of the service
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the service factory for the service.
- *
- * @return the service factory for the service
- */
- public ServiceFactory getServiceFactory() {
- return serviceFactory;
- }
-
- /**
- * Gets the class loader for the service.
- *
- * @return the class loader for the service
- */
- public ClassLoader getClassLoader() {
- return classLoader;
- }
-
- /**
- * Gets the service instance or null if the service doesn't exist.
- *
- * @return the service instance
- */
- public Object getService() {
- return service;
- }
-
- /**
- * Gets the error that was thrown during startup or shutdown. This is available only in error events.
- *
- * @return the error
- */
- public Throwable getCause() {
- return cause;
- }
-
- /**
- * Gets the unsatified dependencies that cause the service to wait. This is available only in waiting events.
- *
- * @return the unsatified dependencies that cause the service to wait
- */
- public Set getUnsatisfiedConditions() {
- return unsatisfiedConditions;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceFactory.java b/kernel/src/java/org/xbean/kernel/ServiceFactory.java
deleted file mode 100644
index b8659973..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceFactory.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-
-/**
- * A service factory is responsible for construction and destruction of a single service. A service factory provides
- * the kernel the start conditions, stopped conditions, owned services and enabled status of the service.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface ServiceFactory {
- /**
- * Gets the types of the service this service factory will create. These types is used to index the service within
- * the kernel. It is a start error to return an object from create service that is not an instance of every type.
- * This is the only type used to index the service, so if the service factory returns a subclass of this type from
- * createService, the subtypes will now be reflected in the index.
- *
- * @return the type of the service this service factory will create
- */
- Class[] getTypes();
-
- /**
- * A restartable service can be started and stopped repeatedly in the kernel. A service that is not restartable
- * immediately enters the RUNNING state when registered with the kernel, and can not be started or stopped.
- *
- * @return true if this service can be started and stopped; false otherwise
- */
- boolean isRestartable();
-
- /**
- * Determines if the service can be instantiated in a kernel. A disabled restartable service can not be
- * started and a disabled non-restartable service can not be loaded into a kernel.
- *
- * @return true if the service factory is enabled; false otherwise
- */
- boolean isEnabled();
-
- /**
- * Sets the enabled status of this service factory. A disabled restartable service can not be
- * started and a disabled non-restartable service can not be loaded into a kernel.
- *
- * @param enabled the new enabled state of this factory
- */
- void setEnabled(boolean enabled);
-
- /**
- * Get an unmodifable snapshot of the conditions that must be satisfied before this service can be started.
- *
- * @return the start conditions of this service
- */
- Set getStartConditions();
-
- /**
- * Adds start condition to this service.
- *
- * @param startCondition the new start condition
- * @throws NullPointerException if startCondition is null
- */
- void addStartCondition(ServiceCondition startCondition) throws NullPointerException;
-
- /**
- * Removes a start condition from this service.
- *
- * @param startCondition the start condition to remove
- * @throws NullPointerException if startCondition is null
- */
- void removeStartCondition(ServiceCondition startCondition) throws NullPointerException;
-
- /**
- * Get an unmodifable snapshot of the conditions that must be satisfied before this service can be stopped.
- *
- * @return the stop conditions of this service
- */
- Set getStopConditions();
-
- /**
- * Adds stop condition to this service.
- *
- * @param stopCondition the new stop condition
- * @throws NullPointerException if stopCondition is null
- */
- void addStopCondition(ServiceCondition stopCondition) throws NullPointerException;
-
- /**
- * Removes a stop condition from this service.
- *
- * @param stopCondition the stop condition to remove
- * @throws NullPointerException if stopCondition is null
- */
- void removeStopCondition(ServiceCondition stopCondition) throws NullPointerException;
-
- /**
- * Gets the names of services owned by this service. This information is used for the startRecursive method on the
- * kernel. When a servcie is started with startRecursive all owned services will be started with startRecursive.
- *
- * @return the names of the services owned by this service
- */
- Set getOwnedServices();
-
- /**
- * Creates the service instance.
- *
- * @param serviceContext context information for the new service
- * @return the service instance
- * @throws Exception if a problem occurs during construction
- */
- Object createService(ServiceContext serviceContext) throws Exception;
-
- /**
- * Destroys the service instance.
- *
- * @param serviceContext the context information for the service
- */
- void destroyService(ServiceContext serviceContext);
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceMonitor.java b/kernel/src/java/org/xbean/kernel/ServiceMonitor.java
deleted file mode 100644
index d1eeed98..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceMonitor.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * This interface is used to monitor service lifecycle events. A ServiceMonitor can be registered with a kernel using
- * {@link Kernel#addServiceMonitor(ServiceMonitor)} or {@link Kernel#addServiceMonitor(ServiceMonitor, ServiceName)}.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface ServiceMonitor {
- /**
- * A new service has been registered with the kernel.
- *
- * @param serviceEvent the event information
- */
- void serviceRegistered(ServiceEvent serviceEvent);
-
- /**
- * A service has entered the STARTING state.
- *
- * @param serviceEvent the event information
- */
- void serviceStarting(ServiceEvent serviceEvent);
-
- /**
- * A service is waiting to start because some start conditions are unsatified.
- *
- * @param serviceEvent the event information
- * @see ServiceEvent#getUnsatisfiedConditions()
- */
- void serviceWaitingToStart(ServiceEvent serviceEvent);
-
- /**
- * An error occured while calling creating the service.
- *
- * @param serviceEvent the event information
- * @see ServiceEvent#getCause()
- */
- void serviceStartError(ServiceEvent serviceEvent);
-
- /**
- * A service has entered the RUNNING state.
- *
- * @param serviceEvent the event information
- */
- void serviceRunning(ServiceEvent serviceEvent);
-
- /**
- * A service has entered the RUNNING state.
- *
- * @param serviceEvent the event information
- */
- void serviceStopping(ServiceEvent serviceEvent);
-
- /**
- * A service is waiting to stop because some stop condition is unsatified.
- *
- * @param serviceEvent the event information
- * @see ServiceEvent#getUnsatisfiedConditions()
- */
- void serviceWaitingToStop(ServiceEvent serviceEvent);
-
- /**
- * An error occured while calling destroying the service.
- *
- * @param serviceEvent the event information
- * @see ServiceEvent#getCause()
- */
- void serviceStopError(ServiceEvent serviceEvent);
-
- /**
- * A service has entered the STOPPED state.
- *
- * @param serviceEvent the event information
- */
- void serviceStopped(ServiceEvent serviceEvent);
-
- /**
- * A service has been unregistered from the kernel.
- *
- * @param serviceEvent the event information
- */
- void serviceUnregistered(ServiceEvent serviceEvent);
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceName.java b/kernel/src/java/org/xbean/kernel/ServiceName.java
deleted file mode 100644
index 1d05537b..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceName.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * The immutable unique name of a service. A proper implementation of ServiceName must have a correct implementation of
- * equals and hashCode. A ServiceName should have one constructor that takes a single String and the toString method
- * should return a String that can be used in the String constructor. This means the following code should work:
- *
- *
- * @param object some object
- * @return true if the object is equivalent to this service name; false otherwise
- */
- boolean equals(Object object);
-
- /**
- * A service name should return a string from toString that can be used in a String constructor.
- *
- * @return the connonical form of this name
- */
- String toString();
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceNotFoundException.java b/kernel/src/java/org/xbean/kernel/ServiceNotFoundException.java
deleted file mode 100644
index 21d240cc..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceNotFoundException.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * A service with the specified name was not found.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceNotFoundException extends Exception {
- private final ServiceName serviceName;
-
- /**
- * Creates a ServiceNotFoundException for the specified service name.
- *
- * @param serviceName the name of the service that was not found.
- */
- public ServiceNotFoundException(ServiceName serviceName) {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- this.serviceName = serviceName;
- }
-
- /**
- * Gets the name of the service that was not found.
- *
- * @return the the name of the service that was not found
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceRegistrationException.java b/kernel/src/java/org/xbean/kernel/ServiceRegistrationException.java
deleted file mode 100644
index 809cdfb8..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceRegistrationException.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * A problem occured while attempting to register or unregister an exception.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceRegistrationException extends Exception {
- private final ServiceName serviceName;
-
- /**
- * Creates a ServiceRegistrationException for the specified service caused by the specified Throwable.
- *
- * @param serviceName the name of the service that was being registered or unregistered.
- * @param cause the reason the registeration problem occured
- */
- public ServiceRegistrationException(ServiceName serviceName, Throwable cause) {
- super(cause);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- this.serviceName = serviceName;
- }
-
- /**
- * Gets the name of the service that had a registration problem.
- *
- * @return the the name of the service that had a registration problem
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/ServiceState.java b/kernel/src/java/org/xbean/kernel/ServiceState.java
deleted file mode 100644
index 8de272a4..00000000
--- a/kernel/src/java/org/xbean/kernel/ServiceState.java
+++ /dev/null
@@ -1,145 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.io.Serializable;
-
-/**
- * The state of services within the Kernel. The state model is directly adapted from the J2EE Management Specification
- * (JSR 77) with the removal of the FAILED state.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public final class ServiceState implements Serializable {
- private static final long serialVersionUID = -2629672602273580572L;
-
- /**
- * This state indicates that the service is attempting to start but has not fully started yet. Normally, a service
- * in this state is waiting for a required service to enter the RUNNING state.
- */
- public static final ServiceState STARTING = new ServiceState((byte) 0, "STARTING");
-
- /**
- * This state indicates that the service is in the normal operational state.
- */
- public static final ServiceState RUNNING = new ServiceState((byte) 1, "RUNNING");
-
- /**
- * This state indicates that the service is attempting to stop but has not fully stopped yet. Normally, a service
- * in this state because another service is still usind this service.
- */
- public static final ServiceState STOPPING = new ServiceState((byte) 2, "STOPPING");
-
- /**
- * This state indicates that the service is stopped and not operational.
- */
- public static final ServiceState STOPPED = new ServiceState((byte) 3, "STOPPED");
-
- /**
- * A quick index for looking up service states.
- */
- private static final ServiceState[] serviceStateIndex = new ServiceState[]{STARTING, RUNNING, STOPPING, STOPPED};
-
- static {
- for (int i = 0; i < serviceStateIndex.length; i++) {
- ServiceState serviceState = serviceStateIndex[i];
- if (serviceState.getIndex() != i) {
- throw new AssertionError(serviceState + " state index is " + serviceState.getIndex() +
- ", but is located at index " + i + " in the serviceStateIndex");
- }
- }
- }
-
- /**
- * Converts the state index into corresponding state name.
- *
- * @param state the state index
- * @return the name of the state
- * @throws IllegalArgumentException if the state index is not 0, 1, 2 or 3
- */
- public static ServiceState getServiceState(int state) throws IllegalArgumentException {
- if (state < 0 || state >= serviceStateIndex.length) {
- throw new IllegalArgumentException("Unknown state " + state);
- }
- return serviceStateIndex[state];
- }
-
- /**
- * Converts the state name in the corresponding state index. This method performs a case insensitive comparison.
- *
- * @param state the state name
- * @return the state index
- * @throws IllegalArgumentException if the state index is not STARTING, RUNNING, STOPPING or FAILED
- */
- public static ServiceState parseServiceState(String state) {
- if (state == null) throw new NullPointerException("state is null");
- if (STARTING.toString().equalsIgnoreCase(state)) {
- return STARTING;
- } else if (RUNNING.toString().equalsIgnoreCase(state)) {
- return RUNNING;
- } else if (STOPPING.toString().equalsIgnoreCase(state)) {
- return STOPPING;
- } else if (STOPPED.toString().equalsIgnoreCase(state)) {
- return STOPPED;
- } else {
- throw new IllegalArgumentException("Unknown state " + state);
- }
- }
-
- private final byte index;
- private final transient String name;
-
- private ServiceState(byte index, String name) {
- this.index = index;
- this.name = name;
- }
-
- /**
- * Gets the unique index of this state. This index can be be used to retrieve this state instance using the
- * getServiceState(int) method.
- *
- * @return the unique index of this state
- */
- public int getIndex() {
- return index;
- }
-
- /**
- * The unique name of this state. This uppercase name can be used to retrieve this state instance using the
- * parseServiceState(String).
- *
- * @return the unique name of this state
- */
- public String getName() {
- return name;
- }
-
- /**
- * Returns the name of this state.
- *
- * @return the unique name of this state
- */
- public String toString() {
- return name;
- }
-
- private Object readResolve() {
- return serviceStateIndex[index];
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/StartStrategies.java b/kernel/src/java/org/xbean/kernel/StartStrategies.java
deleted file mode 100644
index f05990f6..00000000
--- a/kernel/src/java/org/xbean/kernel/StartStrategies.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-
-/**
- * This class contains the built-in common start startegies.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public final class StartStrategies {
- private StartStrategies() {
- }
-
- /**
- * This strategy attempts to immedately start the service. When there are unsatisfied conditions, this strategy
- * will leave the service in the STARTING state, and throw an UnsatisfiedConditionsException
- * to the caller. When there is a start error, the service will be destroyed and the exception will be rethrown to
- * the caller.
- */
- public static final StartStrategy SYNCHRONOUS = new Synchronous();
-
- private static class Synchronous implements StartStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnsatisfiedConditionsException {
- throw new UnsatisfiedConditionsException("Unsatisfied start conditions", serviceName, conditions);
- }
-
- public void startError(ServiceName serviceName, Throwable startError) throws Exception {
- if (startError instanceof Exception) {
- throw (Exception) startError;
- } else if (startError instanceof Error) {
- throw (Error) startError;
- } else {
- throw new AssertionError(startError);
- }
- }
- }
-
- /**
- * This strategy attempts to start the service asynchronously. When there are unsatisfied conditions, this strategy
- * will leave the service in the STARTING state, and caller will not recieve any exceptions.
- * When there is a start error the service will be destroyed adn the exception will be sent to the service montior.
- * The caller will not recieve any start exception.
- */
- public static final StartStrategy ASYNCHRONOUS = new Asynchronous();
-
- private static class Asynchronous implements StartStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) {
- return false;
- }
-
- public void startError(ServiceName serviceName, Throwable startError) {
- }
- }
-
- /**
- * This strategy wait until the service start. This strategy blocks until all unsatisfied conditons
- * are satisfied. When there is a start error, the service will be destroyed and the exception will be rethrown to
- * the caller.
- */
- public static final StartStrategy BLOCK = new Block();
-
- private static class Block implements StartStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) {
- return true;
- }
-
- public void startError(ServiceName serviceName, Throwable startError) throws Exception {
- if (startError instanceof Exception) {
- throw (Exception) startError;
- } else if (startError instanceof Error) {
- throw (Error) startError;
- } else {
- throw new AssertionError(startError);
- }
- }
- }
-
- /**
- * This strategy attempts to start the service immedately. When there are unsatisfied conditions or a start error
- * the dervice will be destroyed and unregistered. In this case an UnsatisfiedConditionsException or
- * the start error will be thrown to the caller.
- */
- public static final StartStrategy UNREGISTER = new Unregister();
-
- private static class Unregister implements StartStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnregisterServiceException {
- UnsatisfiedConditionsException userException = new UnsatisfiedConditionsException("Unsatisfied start conditions", serviceName, conditions);
- throw new UnregisterServiceException(serviceName, userException);
- }
-
- public void startError(ServiceName serviceName, Throwable startError) throws UnregisterServiceException {
- throw new UnregisterServiceException(serviceName, startError);
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/StartStrategy.java b/kernel/src/java/org/xbean/kernel/StartStrategy.java
deleted file mode 100644
index 24190321..00000000
--- a/kernel/src/java/org/xbean/kernel/StartStrategy.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-
-/**
- * The StartStrategy interface is used to assist the kernel in determining how to handle problems that occur while
- * starting a service.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface StartStrategy {
- /**
- * Determines if the kernel should wait for the unsatified conditions to be satisfied.
- *
- * @param serviceName the name of the service that has the unsatisfied condtions
- * @param conditions the unsatisfied condtions
- * @return true if the kernel should wait for the conditions to be satisfied; false if the strategy would like
- * silently leave the service in the starting state
- * @throws UnsatisfiedConditionsException the the strategy would like to leave the service in the starting state
- * and throw an exception the caller
- * @throws UnregisterServiceException if the strategy would like to ignore the unsatisfied conditions and continue to
- * destry the service
- */
- boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnsatisfiedConditionsException, UnregisterServiceException;
-
- /**
- * Handle the start error. The strategy can rethrow the exception, throw an {@link UnregisterServiceException}, or
- * return. If this method rethrows the exception, the service will be destroyed and the exception will be thrown to
- * the caller. If an UnregisterServiceException is thrown, the kernel will unregister the service and rethrow
- * {@link UnregisterServiceException#getCause()}. If this method returns without throwing an exception, the kernel
- * will pass the exception to the service monitor for processing and leave the service in the starting state.
- *
- * @param serviceName the name of the service that has the error
- * @param startError the Exception or Error
- * @throws UnregisterServiceException if the strategy would like the service to be destroyed and unregistered
- * @throws Exception if the strategy would like to destroy the service and throw the exception to the caller
- */
- void startError(ServiceName serviceName, Throwable startError) throws UnregisterServiceException, Exception;
-}
diff --git a/kernel/src/java/org/xbean/kernel/StaticServiceFactory.java b/kernel/src/java/org/xbean/kernel/StaticServiceFactory.java
deleted file mode 100644
index 8bd134f1..00000000
--- a/kernel/src/java/org/xbean/kernel/StaticServiceFactory.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Collections;
-import java.util.Set;
-
-/**
- * A basic service factory that always creates the supplied object.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StaticServiceFactory extends AbstractServiceFactory {
- private final Object service;
-
- /**
- * Creates a non-restartable service factory which will simply returns the specified service from the createService
- * method.
- *
- * @param service the static to which this factory "creates"
- * @throws NullPointerException if service is null
- */
- public StaticServiceFactory(Object service) throws NullPointerException {
- if (service == null) throw new NullPointerException("service is null");
- this.service = service;
- }
-
- public Class[] getTypes() {
- return new Class[]{service.getClass()};
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isRestartable() {
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- public Set getOwnedServices() {
- return Collections.EMPTY_SET;
- }
-
- /**
- * Returns the static service instance.
- *
- * @param serviceContext ignored
- * @return the static service instance
- */
- public Object createService(ServiceContext serviceContext) {
- return service;
- }
-
- /**
- * This method is a noop.
- *
- * @param serviceContext ignored
- */
- public void destroyService(ServiceContext serviceContext) {
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/StopStrategies.java b/kernel/src/java/org/xbean/kernel/StopStrategies.java
deleted file mode 100644
index eba98cc5..00000000
--- a/kernel/src/java/org/xbean/kernel/StopStrategies.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-
-/**
- * This class contains the built-in common stop startegies.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public final class StopStrategies {
- private StopStrategies() {
- }
-
- /**
- * This strategy attempts to immedately stop the service. When there are unsatisfied conditions, this strategy
- * will leave the service in the STOPPING state, and throw an UnsatisfiedConditionsException
- * to the caller.
- */
- public static final StopStrategy SYNCHRONOUS = new Synchronous();
-
- private static class Synchronous implements StopStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnsatisfiedConditionsException {
- throw new UnsatisfiedConditionsException("Unsatisfied stop conditions", serviceName, conditions);
- }
-
- }
-
- /**
- * This strategy attempts to stop the service asynchronously. When there are unsatisfied conditions, this strategy
- * will leave the service in the STOPPING state, and caller will not recieve any exceptions.
- */
- public static final StopStrategy ASYNCHRONOUS = new Asynchronous();
-
- private static class Asynchronous implements StopStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) {
- return false;
- }
-
- }
-
- /**
- * This strategy wait until the service stops. This strategy blocks until all unsatisfied conditons
- * are satisfied.
- */
- public static final StopStrategy BLOCK = new Block();
-
- private static class Block implements StopStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) {
- return true;
- }
-
- }
-
- /**
- * This strategy forceable stops the service. This strategy ignores all unsatisfied conditons.
- */
- public static final StopStrategy FORCE = new Force();
-
- private static class Force implements StopStrategy {
- public boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws ForcedStopException {
- throw new ForcedStopException(serviceName, conditions);
- }
-
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/StopStrategy.java b/kernel/src/java/org/xbean/kernel/StopStrategy.java
deleted file mode 100644
index bf129f76..00000000
--- a/kernel/src/java/org/xbean/kernel/StopStrategy.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-
-/**
- * The StopStrategy interface is used to assist the kernel in determining how to handle problems that occur while
- * stoping a service.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public interface StopStrategy {
- /**
- * Determines if the kernel should wait for the unsatified conditions to be satisfied.
- *
- * @param serviceName the name of the service that has the unsatisfied condtions
- * @param conditions the unsatisfied condtions
- * @return true if the kernel should wait for the conditions to be satisfied; false if the strategy would like
- * silently leave the service in the stopping state
- * @throws UnsatisfiedConditionsException the the strategy would like to leave the service in the stopping state
- * and throw an exception the caller
- * @throws ForcedStopException if the strategy would like to ignore the unsatisfied conditions and continue to
- * destroy the service
- */
- boolean waitForUnsatisfiedConditions(ServiceName serviceName, Set conditions) throws UnsatisfiedConditionsException, ForcedStopException;
-}
diff --git a/kernel/src/java/org/xbean/kernel/StoppedServiceCondition.java b/kernel/src/java/org/xbean/kernel/StoppedServiceCondition.java
deleted file mode 100644
index f820dbb6..00000000
--- a/kernel/src/java/org/xbean/kernel/StoppedServiceCondition.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * This condition that requires another service be in the STOPPED state to be satisfied.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StoppedServiceCondition implements ServiceCondition {
- private final ServiceName dependency;
- private final DependencyServiceMonitor serviceMonitor = new DependencyServiceMonitor();
-
- private ServiceConditionContext context;
- private boolean satisfied = true;
-
- /**
- * Creates a condition that requires the specified service be in the STOPPED state to be satisfied.
- *
- * @param dependency the service that must be stopped
- */
- public StoppedServiceCondition(ServiceName dependency) {
- if (dependency == null) throw new NullPointerException("dependency is null");
- this.dependency = dependency;
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void initialize(ServiceConditionContext context) {
- if (context == null) throw new NullPointerException("context is null");
-
- // if we have no been destroyed, destroy not
- if (this.context != null) {
- destroy();
- }
-
- this.context = context;
-
- satisfied = false;
- context.getKernel().addServiceMonitor(serviceMonitor, dependency);
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized boolean isSatisfied() {
- if (context == null) {
- // we are not initialized so default to true
- return true;
- }
-
- if (!satisfied) {
- try {
- if (context.getKernel().getService(dependency) == ServiceState.RUNNING) {
- satisfied = true;
- context.getKernel().removeServiceMonitor(serviceMonitor);
- }
- } catch (ServiceNotFoundException ignored) {
- // service hasn't registered yet
- }
- }
- return satisfied;
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized void destroy() {
- if (context == null) {
- // we are already destroyed
- return;
- }
-
- context.getKernel().removeServiceMonitor(serviceMonitor);
- context = null;
- satisfied = true;
- }
-
- private class DependencyServiceMonitor extends NullServiceMonitor {
- public void serviceStopped(ServiceEvent serviceEvent) {
- synchronized (StoppedServiceCondition.this) {
- if (context != null) {
- // we aren't running anymore
- return;
- }
-
- if (!satisfied) {
- return;
- }
-
- if (isSatisfied()) {
- context.setSatisfied();
- }
- }
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/StringServiceName.java b/kernel/src/java/org/xbean/kernel/StringServiceName.java
deleted file mode 100644
index b414bab0..00000000
--- a/kernel/src/java/org/xbean/kernel/StringServiceName.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * A simple service name containing a single String.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StringServiceName implements ServiceName {
- /**
- * The strang name of the service.
- */
- private final String name;
-
- /**
- * Create a StringServiceName wrapping specified name.
- *
- * @param name the name of the service
- */
- public StringServiceName(String name) {
- if (name == null) throw new NullPointerException("name is null");
- if (name.length() == 0) throw new IllegalArgumentException("name must be atleast one character long");
- this.name = name;
- }
-
- public int hashCode() {
- return name.hashCode();
- }
-
- public boolean equals(Object obj) {
- if (obj instanceof StringServiceName) {
- StringServiceName stringServiceName = (StringServiceName) obj;
- return name.equals(stringServiceName.name);
- }
- return false;
- }
-
- public String toString() {
- return name;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/UnregisterServiceException.java b/kernel/src/java/org/xbean/kernel/UnregisterServiceException.java
deleted file mode 100644
index 1e2c0812..00000000
--- a/kernel/src/java/org/xbean/kernel/UnregisterServiceException.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-/**
- * Signafies that there was a problem starting a service and the StartStrategies would like the kernel to
- * unregister the service.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class UnregisterServiceException extends Exception {
- private final ServiceName serviceName;
-
- /**
- * Creates a UnregisterServiceException for the specified service name.
- *
- * @param serviceName the name of the service that is to be unregistered
- * @param cause the reason we are unregistering the service
- */
- public UnregisterServiceException(ServiceName serviceName, Throwable cause) {
- super(cause);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- this.serviceName = serviceName;
- }
-
- /**
- * Gets the name of the service that is to be unregistered.
- *
- * @return the service name
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/UnsatisfiedConditionsException.java b/kernel/src/java/org/xbean/kernel/UnsatisfiedConditionsException.java
deleted file mode 100644
index 714ee659..00000000
--- a/kernel/src/java/org/xbean/kernel/UnsatisfiedConditionsException.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Collections;
-import java.util.Set;
-
-/**
- * Signifies that there were unsatified conditions during a start or stop operation. The service is left in the
- * STARTING state or STOPPING state.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class UnsatisfiedConditionsException extends Exception {
- private final ServiceName serviceName;
- private final Set unsatisfiedConditions;
-
- /**
- * Creates an UnsatisfiedConditionsException for the specified service and unsatisfied conditions.
- *
- * @param message information about the unsatisfied conditions
- * @param serviceName the name of the service that has unsatisfied conditions
- * @param unsatisfiedConditions the unsatisfied conditions
- */
- public UnsatisfiedConditionsException(String message, ServiceName serviceName, Set unsatisfiedConditions) {
- super(message + ": serviceName=" + serviceName + ": unsatisfiedConditions=" + unsatisfiedConditions);
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (unsatisfiedConditions == null) throw new NullPointerException("unsatisfiedConditions is null");
- this.serviceName = serviceName;
- this.unsatisfiedConditions = Collections.unmodifiableSet(unsatisfiedConditions);
- }
-
- /**
- * Gets the name of the service that has unsatisfied conditions.
- *
- * @return the service name
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the conditions that were unsatified when the exception was thrown.
- *
- * @return the unsatified conditions that were ignored
- */
- public Set getUnsatisfiedConditions() {
- return unsatisfiedConditions;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/package.html b/kernel/src/java/org/xbean/kernel/package.html
deleted file mode 100644
index 67d6b6c9..00000000
--- a/kernel/src/java/org/xbean/kernel/package.html
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
-Defines the kernel interface for managing services and interfaces the kernel uses to interact with the services.
-
-
\ No newline at end of file
diff --git a/kernel/src/java/org/xbean/kernel/standard/AggregateCondition.java b/kernel/src/java/org/xbean/kernel/standard/AggregateCondition.java
deleted file mode 100644
index 48c94392..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/AggregateCondition.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Condition;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.ServiceCondition;
-import org.xbean.kernel.ServiceName;
-
-/**
- * Aggregates a set of ServiceConditions together so the ServiceManager can treat them as a single unit.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class AggregateCondition {
- private final Kernel kernel;
- private final ServiceName serviceName;
- private final ClassLoader classLoader;
- private final Lock lock;
- private final Map conditions = new HashMap();
- private final Condition satisfiedSignal;
- private boolean destroyed = false;
-
- /**
- * Creates an aggregate condition.
- *
- * @param kernel the kernel in which the service is registered
- * @param serviceName the name of the service
- * @param classLoader the class loader for the service
- * @param lock the lock for the service manager
- * @param conditions the conditions
- */
- public AggregateCondition(Kernel kernel, ServiceName serviceName, ClassLoader classLoader, Lock lock, Set conditions) {
- this.kernel = kernel;
- this.serviceName = serviceName;
- this.classLoader = classLoader;
- this.lock = lock;
- satisfiedSignal = lock.newCondition();
-
- // add the conditions to the registry
- if (conditions == null) throw new NullPointerException("conditions is null");
- for (Iterator iterator = conditions.iterator(); iterator.hasNext();) {
- ServiceCondition serviceCondition = (ServiceCondition) iterator.next();
- addCondition(serviceCondition);
- }
- }
-
- /**
- * Gets a snapshot of the current conditions.
- *
- * @return a snapshot of the current conditions
- */
- protected Set getConditions() {
- return new HashSet(conditions.keySet());
- }
-
- /**
- * Adds a new condition if not already registered.
- *
- * @param condition the new condition
- */
- protected final void addCondition(ServiceCondition condition) {
- if (!conditions.containsKey(condition)) {
- StandardServiceConditionContext context = new StandardServiceConditionContext(kernel, serviceName, classLoader, lock, satisfiedSignal);
- condition.initialize(context);
- conditions.put(condition, context);
- }
- }
-
- /**
- * Removes a condition from the registry if present.
- *
- * @param condition the condition to remove
- */
- protected final void removeCondition(ServiceCondition condition) {
- if (conditions.remove(condition) != null) {
- condition.destroy();
- }
- }
-
- /**
- * Initializes the conditions.
- */
- public void initialize() {
- if (destroyed) throw new IllegalStateException("destroyed");
-
- for (Iterator iterator = conditions.entrySet().iterator(); iterator.hasNext();) {
- Map.Entry entry = (Map.Entry) iterator.next();
- ServiceCondition condition = (ServiceCondition) entry.getKey();
- StandardServiceConditionContext context = (StandardServiceConditionContext) entry.getValue();
- condition.initialize(context);
- }
- }
-
- /**
- * Gets the unsatisfied conditions.
- *
- * @return the unstatisfied conditions
- */
- public Set getUnsatisfied() {
- if (destroyed) throw new IllegalStateException("destroyed");
-
- Set unsatisfied = new HashSet();
- for (Iterator iterator = conditions.entrySet().iterator(); iterator.hasNext();) {
- Map.Entry entry = (Map.Entry) iterator.next();
- ServiceCondition condition = (ServiceCondition) entry.getKey();
- StandardServiceConditionContext context = (StandardServiceConditionContext) entry.getValue();
- if (!context.isSatisfied()) {
- if (condition.isSatisfied()) {
- // the condition is satisfied
- // record this fact in the context
- context.setSatisfied();
- } else {
- unsatisfied.add(condition);
- }
- }
- }
-
- // notify anyone awaiting satisfaction
- if (unsatisfied.isEmpty()) {
- satisfiedSignal.signalAll();
- }
- return unsatisfied;
- }
-
- /**
- * Gets the destroyed status.
- *
- * @return true if this AggregateCondition been destroyed; false otherwise
- */
- public boolean isDestroyed() {
- return destroyed;
- }
-
- /**
- * Destroys all condtions.
- *
- * @return a list of the Exceptions or Errors that occured while destroying the conditon objects.
- */
- public List destroy() {
- List stopErrors = new ArrayList();
- if (!destroyed) {
- destroyed = true;
- for (Iterator iterator = conditions.keySet().iterator(); iterator.hasNext();) {
- ServiceCondition condition = (ServiceCondition) iterator.next();
- try {
- condition.destroy();
- } catch (RuntimeException stopError) {
- stopErrors.add(stopError);
- } catch (Error stopError) {
- stopErrors.add(stopError);
- }
- }
- // notify anyone awaiting satisfaction
- satisfiedSignal.signalAll();
- }
- return stopErrors;
- }
-
- /**
- * Causes the current thread to wait until the conditons is satisfied.
- *
- * @throws InterruptedException if the thread is interrupted
- */
- public void awaitSatisfaction() throws InterruptedException {
- while (!destroyed) {
- if (getUnsatisfied().isEmpty()) {
- return;
- }
- satisfiedSignal.await();
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/AsyncServiceMonitor.java b/kernel/src/java/org/xbean/kernel/standard/AsyncServiceMonitor.java
deleted file mode 100644
index 6a01dbe8..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/AsyncServiceMonitor.java
+++ /dev/null
@@ -1,155 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import edu.emory.mathcs.backport.java.util.concurrent.Executor;
-import org.xbean.kernel.ServiceEvent;
-import org.xbean.kernel.ServiceMonitor;
-
-/**
- * The AsyncServiceMonitor delivers service events to a delegate ServiceMonitor asynchronously using an executor.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class AsyncServiceMonitor implements ServiceMonitor {
- private final ServiceMonitor delegate;
- private final Executor executor;
-
- /**
- * Creates a AsyncServiceMonitor which asynchronously delivers service events to specified delegate
- * ServiceMonitor using the specified executor.
- *
- * @param delegate the service monitor that should recieve the asynchronous events
- * @param executor the executor used to asynchronously deliver the events
- */
- public AsyncServiceMonitor(ServiceMonitor delegate, Executor executor) {
- this.delegate = delegate;
- this.executor = executor;
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceRegistered(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceRegistered(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStarting(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceStarting(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceWaitingToStart(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceWaitingToStart(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStartError(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceStartError(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceRunning(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceRunning(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopping(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceStopping(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceWaitingToStop(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceWaitingToStop(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopError(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceStopError(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopped(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceStopped(serviceEvent);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceUnregistered(final ServiceEvent serviceEvent) {
- executor.execute(new Runnable() {
- public void run() {
- delegate.serviceUnregistered(serviceEvent);
- }
- });
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/KernelMonitorBroadcaster.java b/kernel/src/java/org/xbean/kernel/standard/KernelMonitorBroadcaster.java
deleted file mode 100644
index daa10a1c..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/KernelMonitorBroadcaster.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
-import org.xbean.kernel.KernelMonitor;
-import org.xbean.kernel.ServiceMonitor;
-import org.xbean.kernel.ServiceEvent;
-import org.xbean.kernel.KernelErrorsError;
-
-/**
- * The KernelMonitorBroadcaster broadcasts kernel events to registered kernel monitors.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelMonitorBroadcaster implements KernelMonitor {
- /**
- * The monitors of kernel events.
- */
- private final CopyOnWriteArrayList kernelMonitors = new CopyOnWriteArrayList();
-
- /**
- * Adds a kernel monitor.
- *
- * @param kernelMonitor the kernel monitor to add
- */
- public void addKernelMonitor(KernelMonitor kernelMonitor) {
- kernelMonitors.addIfAbsent(kernelMonitor);
- }
-
- /**
- * Removes a kernel monitor.
- *
- * @param kernelMonitor the kernel monitor to remove
- */
- public void removeKernelMonitor(KernelMonitor kernelMonitor) {
- kernelMonitors.remove(kernelMonitor);
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceNotificationError(ServiceMonitor serviceMonitor, ServiceEvent serviceEvent, Throwable throwable) {
- List errors = new ArrayList();
- for (Iterator iterator = kernelMonitors.iterator(); iterator.hasNext();) {
- KernelMonitor kernelMonitor = (KernelMonitor) iterator.next();
- try {
- kernelMonitor.serviceNotificationError(serviceMonitor, serviceEvent, throwable);
- } catch (RuntimeException ignored) {
- // ignore - we did our best to notify the world
- } catch (Error e) {
- errors.add(e);
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/NonRestartableStopCondition.java b/kernel/src/java/org/xbean/kernel/standard/NonRestartableStopCondition.java
deleted file mode 100644
index c154fd0b..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/NonRestartableStopCondition.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Set;
-
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.ServiceCondition;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceName;
-
-/**
- * A special sub-class of AggregateCondition used to manage the stop conditions of a non-restartable service. This class
- * will update stop conditions to reflect the stop conditions currently registered with the service factory, when the
- * initialized or getUnsatisfied methods are called.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class NonRestartableStopCondition extends AggregateCondition {
- private final ServiceFactory serviceFactory;
-
- /**
- * Creates a NonRestartableStopCondition.
- *
- * @param kernel the kernel in which the service is registered
- * @param serviceName the name of the service
- * @param classLoader the class loader for the service
- * @param lock the lock for the service manager
- * @param serviceFactory the service factory for the service
- */
- public NonRestartableStopCondition(Kernel kernel, ServiceName serviceName, ClassLoader classLoader, Lock lock, ServiceFactory serviceFactory) {
- super(kernel, serviceName, classLoader, lock, Collections.EMPTY_SET);
- this.serviceFactory = serviceFactory;
- }
-
- /**
- * Throws UnsupportedOperationException. Initialize is not a valid operation for a NonRestartableStopCondition
- *
- * @throws UnsupportedOperationException always
- */
- public synchronized void initialize() throws UnsupportedOperationException {
- throw new UnsupportedOperationException("initialize should never be called on a NonRestartableStopCondition");
- }
-
- /**
- * {@inheritDoc}
- */
- public synchronized Set getUnsatisfied() {
- updateConditions();
- return super.getUnsatisfied();
- }
-
- private void updateConditions() {
- if (isDestroyed()) throw new IllegalStateException("destroyed");
-
- Set conditions = getConditions();
-
- // add the new conditions
- Set stopConditions = serviceFactory.getStopConditions();
- for (Iterator iterator = stopConditions.iterator(); iterator.hasNext();) {
- ServiceCondition condition = (ServiceCondition) iterator.next();
- if (!conditions.contains(condition)) {
- addCondition(condition);
- }
- }
-
- // remove the conditions that were dropped
- for (Iterator iterator = conditions.iterator(); iterator.hasNext();) {
- ServiceCondition serviceCondition = (ServiceCondition) iterator.next();
- if (!stopConditions.contains(serviceCondition)) {
- removeCondition(serviceCondition);
- }
- }
- }
-
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/RegistryFutureTask.java b/kernel/src/java/org/xbean/kernel/standard/RegistryFutureTask.java
deleted file mode 100644
index cb22328c..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/RegistryFutureTask.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import edu.emory.mathcs.backport.java.util.concurrent.Callable;
-import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.StopStrategy;
-
-/**
- * RegistryFutureTask preforms service registration and unregistration in a FutureTask.
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-class RegistryFutureTask extends FutureTask implements Comparable {
- private final long serviceId;
- private final ServiceName serviceName;
- private final String taskType;
- private Throwable throwable;
-
- static RegistryFutureTask createRegisterTask(ServiceManager serviceManager) {
- RegisterCallable registerCallable = new RegisterCallable(serviceManager);
- RegistryFutureTask registryFutureTask = new RegistryFutureTask(serviceManager.getServiceId(),
- serviceManager.getServiceName(),
- "RegisterServiceManager",
- registerCallable);
- return registryFutureTask;
- }
-
- static RegistryFutureTask createUnregisterTask(ServiceManager serviceManager, StopStrategy stopStrategy) {
- UnregisterCallable unregisterCallable = new UnregisterCallable(serviceManager, stopStrategy);
- RegistryFutureTask registryFutureTask = new RegistryFutureTask(serviceManager.getServiceId(),
- serviceManager.getServiceName(),
- "UnregisterServiceManager",
- unregisterCallable);
- unregisterCallable.setRegistryFutureTask(registryFutureTask);
- return registryFutureTask;
- }
-
- private RegistryFutureTask(long serviceId, ServiceName serviceName, String taskType, Callable callable) {
- super(callable);
- this.serviceId = serviceId;
- this.serviceName = serviceName;
- this.taskType = taskType;
- }
-
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- public synchronized Throwable getThrowable() {
- return throwable;
- }
-
- private synchronized void setThrowable(Throwable throwable) {
- this.throwable = throwable;
- }
-
- public int hashCode() {
- return (int) (serviceId ^ (serviceId >>> 32));
- }
-
- public boolean equals(Object o) {
- if (o instanceof RegistryFutureTask) {
- return serviceId == ((RegistryFutureTask) o).serviceId;
- }
- return false;
- }
-
- public int compareTo(Object o) {
- RegistryFutureTask registryFutureTask = (RegistryFutureTask) o;
-
- if (serviceId < registryFutureTask.serviceId) {
- return -1;
- } else if (serviceId > registryFutureTask.serviceId) {
- return 1;
- } else {
- return 0;
- }
- }
-
- public String toString() {
- return "[RegistryFutureTask: task=" + taskType + ", serviceName=" + serviceName + "]";
- }
-
-
- private static class RegisterCallable implements Callable {
- private final ServiceManager serviceManager;
-
- private RegisterCallable(ServiceManager serviceManager) {
- this.serviceManager = serviceManager;
- }
-
- public Object call() throws Exception {
- serviceManager.initialize();
- return serviceManager;
- }
- }
-
- private static class UnregisterCallable implements Callable {
- private final ServiceManager serviceManager;
- private final StopStrategy stopStrategy;
- private RegistryFutureTask registryFutureTask;
-
- private UnregisterCallable(ServiceManager serviceManager, StopStrategy stopStrategy) {
- this.serviceManager = serviceManager;
- this.stopStrategy = stopStrategy;
- }
-
- public void setRegistryFutureTask(RegistryFutureTask registryFutureTask) {
- this.registryFutureTask = registryFutureTask;
- }
-
- public Object call() {
- try {
- serviceManager.destroy(stopStrategy);
- return null;
- } catch (Throwable e) {
- // Destroy failed, save the exception so it can be rethrown from the unregister method
- registryFutureTask.setThrowable(e);
-
- // return the service manager so the service remains registered
- return serviceManager;
- }
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/ServiceManager.java b/kernel/src/java/org/xbean/kernel/standard/ServiceManager.java
deleted file mode 100644
index 02ae510e..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/ServiceManager.java
+++ /dev/null
@@ -1,734 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.Arrays;
-import java.util.LinkedHashSet;
-import java.util.Collections;
-
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
-import org.xbean.kernel.ForcedStopException;
-import org.xbean.kernel.IllegalServiceStateException;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.KernelOperationInterruptedException;
-import org.xbean.kernel.KernelOperationTimoutException;
-import org.xbean.kernel.ServiceCondition;
-import org.xbean.kernel.ServiceEvent;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceMonitor;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.ServiceNotFoundException;
-import org.xbean.kernel.ServiceState;
-import org.xbean.kernel.StartStrategies;
-import org.xbean.kernel.StartStrategy;
-import org.xbean.kernel.StopStrategy;
-import org.xbean.kernel.UnregisterServiceException;
-import org.xbean.kernel.UnsatisfiedConditionsException;
-import org.xbean.kernel.InvalidServiceTypeException;
-
-/**
- * The ServiceManager handles the life cycle of a single service. The manager is responsible for gaurenteeing that
- * all start conditions have been satisfied before the service is constructed, and that all stop conditions have been
- * satisfied before the service is destroyed. The ServiceManager can be started and stopped several times, but once
- * destroyed no methods may be called.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceManager implements Comparable {
- /**
- * The kernel in which this service is registered.
- */
- private final Kernel kernel;
-
- /**
- * The unique id of this service in the kernel.
- */
- private final long serviceId;
-
- /**
- * The unique name of this service in the kernel.
- */
- private final ServiceName serviceName;
-
- /**
- * The factory used to create and destroy the service instance.
- */
- private final ServiceFactory serviceFactory;
-
- /**
- * The type of service this service manager will create. This value is cached from the serviceFactory.getT
- */
- private final Set serviceTypes;
-
- /**
- * The class loader for this service.
- */
- private final ClassLoader classLoader;
-
- /**
- * The monitor to which we fire service events. The ServiceManager requires an asynchronous monitor becuse events are
- * fired from within the lock. This helps to reduce complexity but will cause more services to sit in the
- * {@link ServiceState#STARTING} and {@link ServiceState#STOPPING} states since events are propagated in a separate
- * thread.
- */
- private final ServiceMonitor serviceMonitor;
-
- /**
- * The service context given to the service factory. This contans a reference to the kernel, serviceName and
- * classloader.
- */
- private final StandardServiceContext standardServiceContext;
-
- /**
- * Current state of this service.
- */
- private volatile ServiceState state = ServiceState.STOPPED;
-
- /**
- * The time the service was started or 0 if not started.
- */
- private volatile long startTime;
-
- /**
- * The {@link ServiceCondition) objects required to be ready before this service can be completely started.
- */
- private AggregateCondition startCondition;
-
- /**
- * The {@link ServiceCondition) objects required to be ready before this service can be completely stopped.
- */
- private AggregateCondition stopCondition;
-
- /**
- * The service instance.
- */
- private volatile Object service;
-
- /**
- * The single lock we use.
- */
- private final ReentrantLock lock = new ReentrantLock();
-
- /**
- * The maximum duration to wait for the lock.
- */
- private final long timeoutDuration;
-
- /**
- * The unit of measure for the {@link #timeoutDuration}.
- */
- private final TimeUnit timeoutUnits;
-
- /**
- * The name of the operation for which the lock is held; this is used in the reentrant exception message.
- */
- private String currentLockHolderOperation = "NOT-HELD";
-
- /**
- * Sequence number for service event objects.
- */
- private final AtomicLong eventId = new AtomicLong(0);
-
- /**
- * If true, when start is successful we will startRecusrive all of the services owned by this service.
- */
- private boolean recursive = false;
-
- /**
- * Creates a service manager for a single service.
- *
- * @param kernel the kernel in which this wraper will be registered
- * @param serviceId the unique id of this service in the kernel
- * @param serviceName the unique name of this service in the kernel
- * @param serviceFactory the factory used to create and destroy the service instance
- * @param classLoader the class loader for this service
- * @param serviceMonitor the monitor of service events
- * @param timeoutDuration the maximum duration to wait for a lock
- * @param timeoutUnits the unit of measure for the timeoutDuration
- */
- public ServiceManager(Kernel kernel,
- long serviceId,
- ServiceName serviceName,
- ServiceFactory serviceFactory,
- ClassLoader classLoader,
- ServiceMonitor serviceMonitor,
- long timeoutDuration,
- TimeUnit timeoutUnits) {
-
- this.kernel = kernel;
- this.serviceId = serviceId;
- this.serviceName = serviceName;
- this.serviceFactory = serviceFactory;
- this.classLoader = classLoader;
- this.serviceMonitor = serviceMonitor;
- this.timeoutDuration = timeoutDuration;
- this.timeoutUnits = timeoutUnits;
- standardServiceContext = new StandardServiceContext(kernel, serviceName, classLoader);
- serviceTypes = Collections.unmodifiableSet(new LinkedHashSet(Arrays.asList(serviceFactory.getTypes())));
- }
-
- /**
- * Initializes the service.
- *
- * @throws IllegalServiceStateException if the service is not restartable and is disabled
- * @throws UnsatisfiedConditionsException if the service is not restartable and there were unsatisfied start conditions
- * @throws Exception if the service is not restartable and service construction threw an exception
- * @see Kernel#registerService(ServiceName, ServiceFactory, ClassLoader)
- */
- public void initialize() throws IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- if (!serviceFactory.isRestartable() && !serviceFactory.isEnabled()) {
- throw new IllegalServiceStateException("A disabled non-restartable service factory can not be initalized", serviceName);
- }
-
- serviceMonitor.serviceRegistered(createServiceEvent());
-
- // if we are not restartable, we need to start immediately, otherwise we are not going to register this service
- if (!serviceFactory.isRestartable()) {
- try {
- start(false, StartStrategies.UNREGISTER);
- } catch (UnregisterServiceException e) {
- serviceMonitor.serviceUnregistered(createServiceEvent());
- Throwable cause = e.getCause();
- if (cause instanceof Exception) {
- throw (Exception) cause;
- } else if (cause instanceof Error) {
- throw (Error) cause;
- } else {
- throw new AssertionError(cause);
- }
- }
-
- // a non restartable service uses a special stop conditions object that picks up stop conditions as they
- // are added. When the stop() method is called on a non-restartable service all of the stop conditions
- // registered with the service factory are initialized (if not already initialized), and the isSatisfied
- // method is called. This should cause the stop logic of a stop condition to fire.
- lock("initialize");
- try {
- stopCondition = new NonRestartableStopCondition(kernel, serviceName, classLoader, lock, serviceFactory);
- } finally {
- unlock();
- }
- }
- }
-
- /**
- * Attempts to stop and destroy the service.
- *
- * @param stopStrategy the strategy used to determine how to handle unsatisfied stop conditions
- * @throws IllegalServiceStateException is the service did not stop
- * @throws UnsatisfiedConditionsException if there were unsatisfied stop conditions
- * @see Kernel#unregisterService(ServiceName, StopStrategy)
- */
- public void destroy(StopStrategy stopStrategy) throws IllegalServiceStateException, UnsatisfiedConditionsException {
- // if we are not restartable, we need to stop
- try {
- if (!stop(stopStrategy)) {
- throw new IllegalServiceStateException("Service did not stop", serviceName);
- }
- } catch (UnsatisfiedConditionsException e) {
- throw e;
- }
-
- if (!serviceFactory.isRestartable()) {
- lock("destroy");
- try {
- if (state != ServiceState.STOPPED) {
- state = ServiceState.STARTING;
- serviceMonitor.serviceStopping(createServiceEvent());
- if (service != null) {
- try {
- // destroy the service
- serviceFactory.destroyService(standardServiceContext);
- } catch (Throwable e) {
- serviceMonitor.serviceStopError(createErrorServiceEvent(e));
- }
- }
-
- destroyAllConditions(serviceMonitor);
-
- service = null;
- startTime = 0;
- state = ServiceState.STOPPED;
- serviceMonitor.serviceStopped(createServiceEvent());
- }
- } finally {
- unlock();
- }
- }
-
- // cool we can unregistered
- serviceMonitor.serviceUnregistered(createServiceEvent());
- }
-
- /**
- * Gets the unique id of this service in the kernel.
- *
- * @return the unique id of this service in the kernel
- */
- public long getServiceId() {
- return serviceId;
- }
-
- /**
- * Gets the unique name of this service in the kernel.
- *
- * @return the unique name of this servce in the kernel
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * Gets the types of the service that will be managed by this service manager.
- * @return the types of the service
- */
- public Set getServiceTypes() {
- return serviceTypes;
- }
-
- /**
- * Gets the factory used to create and destroy the service instance.
- *
- * @return the factory for the service instance
- * @see Kernel#getServiceFactory(ServiceName)
- */
- public ServiceFactory getServiceFactory() {
- return serviceFactory;
- }
-
- /**
- * Gets the class loader for this service. This class loader is provided to the service factory in the
- * ServiceContext object.
- *
- * @return the classloader for this service
- * @see Kernel#getClassLoaderFor(ServiceName)
- */
- public ClassLoader getClassLoader() {
- return classLoader;
- }
-
- /**
- * Gets the service instance.
- *
- * @return the service instance
- * @see Kernel#getService(ServiceName)
- */
- public Object getService() {
- return service;
- }
-
- /**
- * Gets the current state of this service.
- *
- * @return the current state of this service
- * @see Kernel#getServiceState(ServiceName)
- */
- public ServiceState getState() {
- return state;
- }
-
- /**
- * Gets the time at which this service entered the STARTING state or 0 if the service is STOPPED.
- *
- * @return the start time or 0 if the service is stopped
- * @see Kernel#getServiceStartTime(ServiceName)
- */
- public long getStartTime() {
- return startTime;
- }
-
- /**
- * Attempts to starts the service.
- *
- * @param recursive if start is successful should we start recursive the services owned by this servic
- * @param startStrategy the strategy used to determine how to handle unsatisfied start conditions and start errors
- * @throws IllegalServiceStateException if the service is in a state in which it can not be started
- * @throws UnregisterServiceException if the kernel should unregister this service
- * @throws UnsatisfiedConditionsException if there were unsatisfied start conditions
- * @throws Exception it service creation threw an exception
- * @see Kernel#startService(ServiceName)
- * @see Kernel#startServiceRecursive(ServiceName)
- */
- public void start(boolean recursive, StartStrategy startStrategy) throws IllegalServiceStateException, UnregisterServiceException, UnsatisfiedConditionsException, Exception {
- // verify that it is possible to start this service in the current state before obtaining the lock
- if (!verifyStartable(state)) {
- if (recursive) {
- startOwnedServices(startStrategy);
- }
- return;
- }
-
- boolean shouldStartRecursive = false;
- lock("start");
- try {
- // update the recursive flag
- this.recursive = this.recursive || recursive;
-
- Throwable startError = null;
- try {
- //
- // Loop until all start conditions have been satified. The start strategy can break this loop.
- //
- boolean satisfied = false;
- while (!satisfied) {
- // do we still want to start?
- if (!verifyStartable(state)) {
- // assume someone else called startOwnedServices
- return;
- }
-
- // if we are in the STOPPED state, we need to move to the STARTING state
- if (state == ServiceState.STOPPED) {
- // we are now officially starting
- state = ServiceState.STARTING;
- serviceMonitor.serviceStarting(createServiceEvent());
-
- // initialize the start conditions
- startCondition = new AggregateCondition(kernel, serviceName, classLoader, lock, serviceFactory.getStartConditions());
- startCondition.initialize();
- }
-
- // are we satisfied?
- Set unsatisfiedConditions = startCondition.getUnsatisfied();
- satisfied = unsatisfiedConditions.isEmpty();
- if (!satisfied) {
- // if the stragegy wants us to wait for conditions to be satisfied, it will return true
- if (startStrategy.waitForUnsatisfiedConditions(serviceName, unsatisfiedConditions)) {
- // wait for satisfaction and loop
- startCondition.awaitSatisfaction();
- } else {
- // no wait, notify the monitor and exit
- serviceMonitor.serviceWaitingToStart(createWaitingServiceEvent(unsatisfiedConditions));
- return;
- }
- }
- }
-
- // we are ready to create the service
- service = serviceFactory.createService(standardServiceContext);
-
- // verify that the service implements all of the types
- if (service == null) {
- throw new NullPointerException("Service factory return null from createService for service " + serviceName);
- }
- for (Iterator iterator = serviceTypes.iterator(); iterator.hasNext();) {
- Class type = (Class) iterator.next();
- if (!type.isInstance(service)) {
- throw new InvalidServiceTypeException(serviceName, type, service.getClass());
- }
- }
-
- // success transition to running
- startTime = System.currentTimeMillis();
- state = ServiceState.RUNNING;
- serviceMonitor.serviceRunning(createServiceEvent());
-
- // should we recursively start our children
- shouldStartRecursive = this.recursive || recursive;
- this.recursive = false;
- } catch (UnsatisfiedConditionsException e) {
- // thrown from waitForUnsatisfiedConditions
- throw e;
- } catch (IllegalServiceStateException e) {
- // this can be thrown while awaiting satisfaction
- throw e;
- } catch (Exception e) {
- startError = e;
- } catch (Error e) {
- startError = e;
- }
-
- if (startError != null) {
- try {
- if (startError instanceof UnregisterServiceException) {
- throw (UnregisterServiceException) startError;
- } else {
- // the strategy will normally rethrow the startError, but if it doesn't notify the service monitor
- startStrategy.startError(serviceName, startError);
- serviceMonitor.serviceStartError(createErrorServiceEvent(startError));
- }
- } finally {
- // we are now STOPPING
- state = ServiceState.STOPPING;
- serviceMonitor.serviceStopping(createServiceEvent());
-
- // clean up the conditons
- destroyAllConditions(serviceMonitor);
-
- // transition to the STOPPED state
- service = null;
- startTime = 0;
- state = ServiceState.STOPPED;
- serviceMonitor.serviceStopped(createServiceEvent());
- }
- }
- } finally {
- unlock();
- }
-
-
- // startRecursive all of the owned services
- if (shouldStartRecursive) {
- startOwnedServices(startStrategy);
- }
- }
-
- private void startOwnedServices(StartStrategy startStrategy) throws IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- Set ownedServices = serviceFactory.getOwnedServices();
- if (ownedServices == null) throw new NullPointerException("serviceFactory.getOwnedServices() returned null");
- for (Iterator iterator = ownedServices.iterator(); iterator.hasNext();) {
- ServiceName ownedService = (ServiceName) iterator.next();
- try {
- kernel.startServiceRecursive(ownedService, startStrategy);
- } catch (ServiceNotFoundException ignored) {
- // this is ok -- service unregistered
- } catch (IllegalServiceStateException ignored) {
- // ownedService is disabled or stopping -- anyway we don't care
- }
- }
- }
-
- /**
- * Verifies that the service is startable. This can be used out side a lock to avoid unecessary locking.
- *
- * @param state the state of the service
- * @return true if it is possible to start a service in the specifiec state
- * @throws IllegalServiceStateException if it is illegal to start a service in the specified state
- */
- private boolean verifyStartable(ServiceState state) throws IllegalServiceStateException {
- // if we are alredy in the running state, there is nothing to do
- if (state == ServiceState.RUNNING) {
- return false;
- }
-
- // if we are in the stopping states, that is an error
- if (state == ServiceState.STOPPING) {
- throw new IllegalServiceStateException("A stopping service can not be started", serviceName);
- }
-
- // is this service enabled?
- if (state == ServiceState.STOPPED && !serviceFactory.isEnabled()) {
- throw new IllegalServiceStateException("Service is disabled", serviceName);
- }
-
- return true;
- }
-
- /**
- * Attempts to stop the service.
- *
- * @param stopStrategy the strategy used to determine how to handle unsatisfied stop conditions
- * @return true if the service was sucessfully stopped; false otherwise
- * @throws UnsatisfiedConditionsException if there were unsatisfied stop conditions
- * @see Kernel#stopService(ServiceName)
- */
- public boolean stop(StopStrategy stopStrategy) throws UnsatisfiedConditionsException {
- // check that we aren't already stopped before attempting to acquire the lock
- ServiceState initialState = state;
- if (initialState == ServiceState.STOPPED) {
- return true;
- }
-
- lock("stop");
- try {
- try {
- //
- // Loop until all stop conditions have been satified. The stop strategy can break this loop.
- //
- boolean satisfied = false;
- while (!satisfied) {
- // do we still want to stop?
- if (state == ServiceState.STOPPED) {
- return true;
- }
-
- // if we are not the STOPPING state, transition to it
- // we check on the stopConditions variable because non-restartable services preset this in the
- // intialization method
- if (stopCondition == null) {
- // we are not officially stopping
- serviceMonitor.serviceStopping(createServiceEvent());
- state = ServiceState.STOPPING;
-
- // initialize all of the stop conditions
- stopCondition = new AggregateCondition(kernel, serviceName, classLoader, lock, serviceFactory.getStopConditions());
- stopCondition.initialize();
- }
-
- // are we satisfied?
- Set unsatisfiedConditions = stopCondition.getUnsatisfied();
- satisfied = unsatisfiedConditions.isEmpty();
- if (!satisfied) {
- // if the stragegy wants us to wait for conditions to be satisfied, it will return true
- if (stopStrategy.waitForUnsatisfiedConditions(serviceName, unsatisfiedConditions)) {
- // wait for satisfaction and loop
- stopCondition.awaitSatisfaction();
- } else {
- // no wait, notify the monitor and exit
- serviceMonitor.serviceWaitingToStop(createWaitingServiceEvent(unsatisfiedConditions));
- return false;
- }
- }
- }
- } catch (UnsatisfiedConditionsException e) {
- throw e;
- } catch (ForcedStopException e) {
- serviceMonitor.serviceStopError(createErrorServiceEvent(e));
- } catch (Exception e) {
- serviceMonitor.serviceStopError(createErrorServiceEvent(e));
- } catch (Error e) {
- serviceMonitor.serviceStopError(createErrorServiceEvent(e));
- }
-
- if (serviceFactory.isRestartable()) {
- if (service != null) {
- try {
- // destroy the service
- serviceFactory.destroyService(standardServiceContext);
- } catch (Throwable e) {
- serviceMonitor.serviceStopError(createErrorServiceEvent(e));
- }
- }
-
- destroyAllConditions(serviceMonitor);
-
- service = null;
- startTime = 0;
- state = ServiceState.STOPPED;
- serviceMonitor.serviceStopped(createServiceEvent());
- }
- return true;
- } finally {
- unlock();
- }
- }
-
- private void destroyAllConditions(ServiceMonitor monitor) {
- if (!lock.isHeldByCurrentThread()) {
- throw new IllegalStateException("Current thread must hold lock before calling destroyAllConditions");
- }
-
- if (startCondition != null) {
- List errors = startCondition.destroy();
- // errors from destroying the start conditions are stop errors because destroy is only called while
- // stopping the service
- for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
- Throwable stopError = (Throwable) iterator.next();
- monitor.serviceStopError(createErrorServiceEvent(stopError));
- }
- startCondition = null;
- }
- if (stopCondition != null) {
- List errors = stopCondition.destroy();
- for (Iterator iterator = errors.iterator(); iterator.hasNext();) {
- Throwable stopError = (Throwable) iterator.next();
- monitor.serviceStopError(createErrorServiceEvent(stopError));
- }
- stopCondition = null;
- }
- }
-
- /**
- * Obtain the lock for the specified operation.
- *
- * @param operationName name of the operation that lock will be used for - this is only used for exception messages
- * @throws IllegalStateException if thread tries to reenter while holding the lock
- * @throws KernelOperationTimoutException if lock could not be obtained in {@link #timeoutDuration} {@link #timeoutUnits}
- * @throws KernelOperationInterruptedException if the thread was interrupted while waiting for the lock
- */
- private void lock(String operationName) throws IllegalStateException, KernelOperationTimoutException, KernelOperationInterruptedException {
- if (lock.isHeldByCurrentThread()) {
- throw new IllegalStateException("Current thread holds lock for " + currentLockHolderOperation +
- " and lock can not be reacquired for " + operationName + " on " + serviceName);
- }
-
- try {
- if (!lock.tryLock(timeoutDuration, timeoutUnits)) {
- throw new KernelOperationTimoutException("Could not obtain lock for " + operationName + " operation on " +
- serviceName + " within " + timeoutDuration + " " + timeoutUnits.toString().toLowerCase(),
- serviceName,
- operationName);
- }
- currentLockHolderOperation = operationName;
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException("Interrupted while attempting to obtain lock for " + operationName +
- " operation on " + serviceName,
- e,
- serviceName,
- operationName);
-
- }
- }
-
- /**
- * Unlock the lock and clear the currentLockHolderOperation name.
- */
- private void unlock() {
- if (!lock.isHeldByCurrentThread()) {
- throw new IllegalMonitorStateException("Not owner");
- }
-
- currentLockHolderOperation = "NOT-HELD";
- lock.unlock();
- }
-
- private ServiceEvent createServiceEvent() {
- return new ServiceEvent(eventId.getAndIncrement(), kernel, serviceName, serviceFactory, classLoader, service, null, null);
- }
-
- private ServiceEvent createWaitingServiceEvent(Set unsatisfiedConditions) {
- return new ServiceEvent(eventId.getAndIncrement(), kernel, serviceName, serviceFactory, classLoader, service, null, unsatisfiedConditions);
- }
-
- private ServiceEvent createErrorServiceEvent(Throwable cause) {
- return new ServiceEvent(eventId.getAndIncrement(), kernel, serviceName, serviceFactory, classLoader, null, cause, null);
- }
-
- public int hashCode() {
- return (int) (serviceId ^ (serviceId >>> 32));
- }
-
- public boolean equals(Object o) {
- if (o instanceof ServiceManager) {
- return serviceId == ((ServiceManager)o).serviceId;
- }
- return false;
- }
-
- public int compareTo(Object o) {
- ServiceManager serviceManager = (ServiceManager) o;
-
- if (serviceId < serviceManager.serviceId) {
- return -1;
- } else if (serviceId > serviceManager.serviceId) {
- return 1;
- } else {
- return 0;
- }
- }
-
- public String toString() {
- return "[ServiceManager: serviceId=" + serviceId + ", serviceName=" + serviceName + ", state=" + state + "]";
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/ServiceManagerFactory.java b/kernel/src/java/org/xbean/kernel/standard/ServiceManagerFactory.java
deleted file mode 100644
index 4dc4998d..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/ServiceManagerFactory.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import edu.emory.mathcs.backport.java.util.concurrent.Executor;
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceName;
-
-/**
- * The ServiceManagerFactory handles the construction ServiceManagers.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceManagerFactory {
- /**
- * The kernel in which the service will be bound.
- */
- private final Kernel kernel;
-
- /**
- * This monitor broadcasts events to the listeners registered for service.
- */
- private final ServiceMonitorBroadcaster serviceMonitor;
-
- /**
- * Events service events are sent asynchronously using this executor.
- */
- private final Executor serviceExecutor;
-
- /**
- * The maximum duration to wait for a service event to complete.
- */
- private final long timeoutDuration;
-
- /**
- * The unit of measure for the {@link #timeoutDuration}.
- */
- private final TimeUnit timeoutUnits;
-
- /**
- * Creates a ServiceManagerFactory.
- *
- * @param kernel the kernel in which the service will be registered
- * @param serviceMonitor the service monitor used for all services created by this factory
- * @param serviceExecutor the executor available to the service manager
- * @param timeoutDuration the maximum duration to wait for a service event to complete
- * @param timeoutUnits the unit of measure for the timeoutDuration
- */
- public ServiceManagerFactory(Kernel kernel, ServiceMonitorBroadcaster serviceMonitor, Executor serviceExecutor, long timeoutDuration, TimeUnit timeoutUnits) {
- this.kernel = kernel;
- this.serviceMonitor = serviceMonitor;
- this.serviceExecutor = serviceExecutor;
- this.timeoutDuration = timeoutDuration;
- this.timeoutUnits = timeoutUnits;
- }
-
- /**
- * Creates a ServiceManager.
- *
- * @param serviceId the id of the service
- * @param serviceName the name of the service
- * @param serviceFactory the factory for the service
- * @param classLoader the classloader for the service
- * @return a new service manager
- */
- public ServiceManager createServiceManager(long serviceId, ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader) {
- return new ServiceManager(kernel,
- serviceId,
- serviceName,
- serviceFactory,
- classLoader,
- new AsyncServiceMonitor(serviceMonitor, serviceExecutor),
- timeoutDuration,
- timeoutUnits);
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/ServiceManagerRegistry.java b/kernel/src/java/org/xbean/kernel/standard/ServiceManagerRegistry.java
deleted file mode 100644
index e13051dc..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/ServiceManagerRegistry.java
+++ /dev/null
@@ -1,592 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedSet;
-import java.util.TreeSet;
-
-import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
-import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
-import org.xbean.kernel.IllegalServiceStateException;
-import org.xbean.kernel.KernelErrorsError;
-import org.xbean.kernel.KernelOperationInterruptedException;
-import org.xbean.kernel.ServiceAlreadyExistsException;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.ServiceNotFoundException;
-import org.xbean.kernel.ServiceRegistrationException;
-import org.xbean.kernel.StopStrategies;
-import org.xbean.kernel.StopStrategy;
-import org.xbean.kernel.UnsatisfiedConditionsException;
-
-/**
- * The StandardServiceRegistry manages the registration of ServiceManagers for the kernel.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceManagerRegistry {
- /**
- * The sequence used for the serviceId assigned to service managers.
- */
- private final AtomicLong serviceId = new AtomicLong(1);
-
- /**
- * The factory used to create service managers.
- */
- private final ServiceManagerFactory serviceManagerFactory;
-
- /**
- * The registered service managers.
- */
- private final Map serviceManagers = new HashMap();
-
- /**
- * The service managers indexed by the service type. This map is populated when a service enters the running state.
- */
- private final Map serviceManagersByType = new HashMap();
-
- /**
- * Creates a ServiceManagerRegistry that uses the specified service manager factory to create new service managers.
- *
- * @param serviceManagerFactory the factory for new service managers
- */
- public ServiceManagerRegistry(ServiceManagerFactory serviceManagerFactory) {
- this.serviceManagerFactory = serviceManagerFactory;
- }
-
- /**
- * Stops and destroys all services service managers. This method will FORCE stop the services if necessary.
- *
- * @throws KernelErrorsError if any errors occur while stopping or destroying the service managers
- */
- public void destroy() throws KernelErrorsError {
- // we gather all errors that occur during shutdown and throw them as on huge exception
- List errors = new ArrayList();
-
- List managerFutures;
- synchronized (serviceManagers) {
- managerFutures = new ArrayList(serviceManagers.values());
- serviceManagers.clear();
-
- }
-
- List managers = new ArrayList(managerFutures.size());
- for (Iterator iterator = managerFutures.iterator(); iterator.hasNext();) {
- RegistryFutureTask registryFutureTask = (RegistryFutureTask) iterator.next();
- try {
- managers.add(registryFutureTask.get());
- } catch (InterruptedException e) {
- // ignore -- this should not happen
- errors.add(new AssertionError(e));
- } catch (ExecutionException e) {
- // good -- one less manager to deal with
- }
- }
-
- // Be nice and try to stop asynchronously
- errors.addAll(stopAll(managers, StopStrategies.ASYNCHRONOUS));
-
- // Be really nice and try to stop asynchronously again
- errors.addAll(stopAll(managers, StopStrategies.ASYNCHRONOUS));
-
- // We have been nice enough now nuke them
- errors.addAll(stopAll(managers, StopStrategies.FORCE));
-
- // All managers are gaurenteed to be destroyed now
- for (Iterator iterator = managers.iterator(); iterator.hasNext();) {
- ServiceManager serviceManager = (ServiceManager) iterator.next();
- try {
- serviceManager.destroy(StopStrategies.FORCE);
- } catch (UnsatisfiedConditionsException e) {
- // this should not happen, because we force stopped
- errors.add(new AssertionError(e));
- } catch (IllegalServiceStateException e) {
- // this should not happen, because we force stopped
- errors.add(new AssertionError(e));
- } catch (RuntimeException e) {
- errors.add(new AssertionError(e));
- } catch (Error e) {
- errors.add(new AssertionError(e));
- }
- }
-
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- private List stopAll(List managers, StopStrategy stopStrategy) {
- List errors = new ArrayList();
- for (Iterator iterator = managers.iterator(); iterator.hasNext();) {
- ServiceManager serviceManager = (ServiceManager) iterator.next();
- try {
- serviceManager.stop(stopStrategy);
- } catch (UnsatisfiedConditionsException e) {
- // this should not happen in with an asynchronous strategy
- errors.add(new AssertionError(e));
- } catch (RuntimeException e) {
- errors.add(new AssertionError(e));
- } catch (Error e) {
- errors.add(new AssertionError(e));
- }
- }
- return errors;
- }
-
- /**
- * Determines if there is a service registered under the specified name.
- *
- * @param serviceName the unique name of the service
- * @return true if there is a service registered with the specified name; false otherwise
- */
- public boolean isRegistered(ServiceName serviceName) {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
-
- RegistryFutureTask registryFutureTask;
- synchronized (serviceManagers) {
- registryFutureTask = (RegistryFutureTask) serviceManagers.get(serviceName);
- }
- try {
- // the service is registered if we have a non-null future value
- return registryFutureTask != null && registryFutureTask.get() != null;
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "isRegistered");
- } catch (ExecutionException e) {
- return false;
- }
- }
-
- /**
- * Gets the service manager registered under the specified name.
- *
- * @param serviceName the unique name of the service
- * @return the ServiceManager
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- */
- public ServiceManager getServiceManager(ServiceName serviceName) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
-
- RegistryFutureTask registryFutureTask;
- synchronized (serviceManagers) {
- registryFutureTask = (RegistryFutureTask) serviceManagers.get(serviceName);
- }
-
- // this service has no future
- if (registryFutureTask == null) {
- throw new ServiceNotFoundException(serviceName);
- }
-
- try {
- ServiceManager serviceManager = (ServiceManager) registryFutureTask.get();
- if (serviceManager == null) {
- throw new ServiceNotFoundException(serviceName);
- }
- return serviceManager;
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "getServiceManager");
- } catch (ExecutionException e) {
- // registration threw an exception which means it didn't register
- throw new ServiceNotFoundException(serviceName);
- }
- }
-
- /**
- * Gets the first registered service manager that creates an instance of the specified type, or null if no service
- * managers create an instance of the specified type.
- *
- * @param type the of the desired service
- * @return the first registered service manager that creates an instance of the specified type, or null if none found
- */
- public ServiceManager getServiceManager(Class type) {
- SortedSet serviceManagerFutures = getServiceManagerFutures(type);
- for (Iterator iterator = serviceManagerFutures.iterator(); iterator.hasNext();) {
- RegistryFutureTask registryFutureTask = (RegistryFutureTask) iterator.next();
- try {
- ServiceManager serviceManager = (ServiceManager) registryFutureTask.get();
- if (serviceManager != null) {
- return serviceManager;
- }
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, registryFutureTask.getServiceName(), "getServiceManagers(java.lang.Class)");
- } catch (ExecutionException ignored) {
- // registration threw an exception which means it didn't register
- }
- }
- return null;
- }
-
- /**
- * Gets all service managers that create an instances of the specified type, or an empty list if no service
- * managers create an instance of the specified type.
- *
- * @param type the of the desired service managers
- * @return all service managers that create an instances of the specified type, or an empty list if none found
- */
- public List getServiceManagers(Class type) {
- SortedSet serviceManagerFutures = getServiceManagerFutures(type);
- List serviceManagers = new ArrayList(serviceManagerFutures.size());
- for (Iterator iterator = serviceManagerFutures.iterator(); iterator.hasNext();) {
- RegistryFutureTask registryFutureTask = (RegistryFutureTask) iterator.next();
- try {
- ServiceManager serviceManager = (ServiceManager) registryFutureTask.get();
- if (serviceManager != null) {
- serviceManagers.add(serviceManager);
- }
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, registryFutureTask.getServiceName(), "getServiceManagers(java.lang.Class)");
- } catch (ExecutionException ignored) {
- // registration threw an exception which means it didn't register
- }
- }
- return serviceManagers;
- }
-
- /**
- * Gets the first registed and running service that is an instance of the specified type, or null if no instances
- * of the specified type are running.
- *
- * @param type the of the desired service
- * @return the first registed and running service that is an instance of the specified type or null if none found
- */
- public synchronized Object getService(Class type) {
- SortedSet serviceManagerFutures = getServiceManagerFutures(type);
- for (Iterator iterator = serviceManagerFutures.iterator(); iterator.hasNext();) {
- RegistryFutureTask registryFutureTask = (RegistryFutureTask) iterator.next();
- try {
- ServiceManager serviceManager = (ServiceManager) registryFutureTask.get();
- if (serviceManager != null) {
- Object service = serviceManager.getService();
- if (service != null) {
- return service;
- }
- }
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, registryFutureTask.getServiceName(), "getService(java.lang.Class)");
- } catch (ExecutionException ignored) {
- // registration threw an exception which means it didn't register
- }
- }
- return null;
- }
-
- /**
- * Gets the all of running service that are an instances of the specified type, or an empty list if no instances
- * of the specified type are running.
- *
- * @param type the of the desired service
- * @return the all of running service that are an instances of the specified type, or an empty list if none found
- */
- public synchronized List getServices(Class type) {
- List serviceManagers = getServiceManagers(type);
- List services = new ArrayList(serviceManagers.size());
- for (Iterator iterator = serviceManagers.iterator(); iterator.hasNext();) {
- ServiceManager serviceManager = (ServiceManager) iterator.next();
- if (serviceManager != null) {
- Object service = serviceManager.getService();
- if (service != null) {
- services.add(service);
- }
- }
- }
- return services;
- }
-
- private SortedSet getServiceManagerFutures(Class type) {
- SortedSet serviceManagerFutures;
- synchronized (serviceManagers) {
- serviceManagerFutures = (SortedSet) serviceManagersByType.get(type);
- if (serviceManagerFutures != null) {
- serviceManagerFutures = new TreeSet(serviceManagerFutures);
- } else {
- serviceManagerFutures = new TreeSet();
- }
- }
- return serviceManagerFutures;
- }
-
- /**
- * Creates a ServiceManager and registers it under the specified name. If the service is restartable, it will
- * enter the server in the STOPPED state. If a service is not restartable, the service manager will assure that all
- * dependencies are satisfied and service will immediately enter in the RUNNING state. If a
- * dependency for a non-restartable service is not immediately satisfiable, this method will throw a
- * ServiceRegistrationException.
- *
- * @param serviceName the unique name of the service
- * @param serviceFactory the factory used to create the service
- * @param classLoader the class loader to use for this service
- * @throws ServiceAlreadyExistsException if service is already registered with the specified name
- * @throws ServiceRegistrationException if the service is not restartable and an error occured while starting the service
- */
- public void registerService(ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader) throws ServiceAlreadyExistsException, ServiceRegistrationException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (serviceFactory == null) throw new NullPointerException("serviceFactory is null");
- if (classLoader == null) throw new NullPointerException("classLoader is null");
-
- if (!serviceFactory.isEnabled()) {
- throw new ServiceRegistrationException(serviceName,
- new IllegalServiceStateException("A disabled non-restartable service factory can not be registered", serviceName));
- }
-
- RegistryFutureTask registrationTask = null;
-
- //
- // This loop will continue until we put our registrationTask in the serviceManagers map. If at any point,
- // we discover that there is already a service registered under the specified service name, we will throw
- // a ServiceAlreadyExistsException exiting this method.
- //
- while (registrationTask == null) {
- RegistryFutureTask existingRegistration;
- synchronized (serviceManagers) {
- existingRegistration = (RegistryFutureTask) serviceManagers.get(serviceName);
-
- // if we do not have an existing registration or the existing registration task is complete
- // we can create the new registration task; otherwise we need to wait for the existing registration to
- // finish out side of the synchronized lock on serviceManagers.
- if (existingRegistration == null || existingRegistration.isDone()) {
- // if we have a valid existing registration, throw a ServiceAlreadyExistsException
- if (existingRegistration != null) {
- try {
- boolean alreadyRegistered = (existingRegistration.get() != null);
- if (alreadyRegistered) {
- throw new ServiceAlreadyExistsException(serviceName);
- }
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "registerService");
- } catch (ExecutionException e) {
- // the previous registration threw an exception.. we can continure as normal
- }
- }
-
- // we are ready to register our serviceManager
- existingRegistration = null;
- ServiceManager serviceManager = serviceManagerFactory.createServiceManager(serviceId.getAndIncrement(),
- serviceName,
- serviceFactory,
- classLoader);
- registrationTask = RegistryFutureTask.createRegisterTask(serviceManager);
- serviceManagers.put(serviceName, registrationTask);
- addTypeIndex(serviceManager, registrationTask);
- }
- }
-
- // If there is an unfinished exiting registration task, wait until it is done executing
- if (existingRegistration != null) {
- try {
- existingRegistration.get();
- // we don't throw an error here because we want to check in the synchronized block that this
- // future is still registered in the serviceManagers map
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "registerService");
- } catch (ExecutionException e) {
- // good
- }
- }
- }
-
- // run our registration task and check the results
- registrationTask.run();
- try {
- // if initialization completed successfully, this method will not throw an exception
- registrationTask.get();
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "registerService");
- } catch (ExecutionException e) {
- // registration failed, remove our task
- synchronized (serviceManagers) {
- // make sure our task is still the registered one
- if (serviceManagers.get(serviceName) == registrationTask) {
- serviceManagers.remove(serviceName);
- removeTypeIndex(registrationTask);
- }
- }
- throw new ServiceRegistrationException(serviceName, e.getCause());
- }
- }
-
- /**
- * Stops and destorys the ServiceManager and then unregisters it. The ServiceManagerRegistry will attempt to stop
- * the service using the specified stop strategy, but if the service can not be stopped a
- * ServiceRegistrationException will be thrown containing either an UnsatisfiedConditionsException or an
- * IllegalServiceStateException.
- *
- * @param serviceName the unique name of the service
- * @param stopStrategy the strategy that determines how unsatisfied conditions are handled
- * @throws ServiceNotFoundException if there is no service registered under the specified name
- * @throws ServiceRegistrationException if the service could not be stopped
- */
- public void unregisterService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, ServiceRegistrationException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (stopStrategy == null) throw new NullPointerException("stopStrategy is null");
-
- RegistryFutureTask unregistrationTask = null;
-
- //
- // This loop will continue until we put our unregistrationTask in the serviceManagers map. If at any point,
- // we discover that there actually is not a service registered under the specified service name, we will throw
- // a ServiceNotFoundException exiting this method.
- //
- while (unregistrationTask == null) {
- RegistryFutureTask existingRegistration;
- synchronized (serviceManagers) {
- existingRegistration = (RegistryFutureTask) serviceManagers.get(serviceName);
- if (existingRegistration == null) {
- throw new ServiceNotFoundException(serviceName);
- }
-
- // if existing registration is done running, we can destroy it
- if (existingRegistration.isDone()) {
- ServiceManager serviceManager = null;
- try {
- serviceManager = (ServiceManager) existingRegistration.get();
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "unregisterService");
- } catch (ExecutionException e) {
- // good
- }
-
- // if there isn't a registered manager that is an exception
- if (serviceManager == null) {
- throw new ServiceNotFoundException(serviceName);
- }
-
- // we are ready to register our serviceManager
- existingRegistration = null;
- unregistrationTask = RegistryFutureTask.createUnregisterTask(serviceManager, stopStrategy);
- serviceManagers.put(serviceName, unregistrationTask);
- addTypeIndex(serviceManager, unregistrationTask);
- }
- }
-
-
- // If there is an unfinished exiting registration task, wait until it is done executing
- if (existingRegistration != null) {
- try {
- existingRegistration.get();
- // we don't throw an error here because we want to check in the synchronized block that this
- // future is still registered in the serviceManagers map
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "unregisterService");
- } catch (ExecutionException e) {
- // good
- }
- }
- }
-
- unregistrationTask.run();
- try {
- // if get returns any value other then null, the unregistration failed
- if (unregistrationTask.get() == null) {
- // unregistration was successful, remove the furuture object
- synchronized (serviceManagers) {
- // make sure our task is still the registered one
- if (serviceManagers.get(serviceName) == unregistrationTask) {
- serviceManagers.remove(serviceName);
- removeTypeIndex(unregistrationTask);
- }
- }
- } else {
- synchronized (unregistrationTask) {
- // the root exception is contained in the exception handle
- throw new ServiceRegistrationException(serviceName, unregistrationTask.getThrowable());
- }
- }
- } catch (InterruptedException e) {
- throw new KernelOperationInterruptedException(e, serviceName, "unregisterService");
- } catch (ExecutionException e) {
- // this won't happen
- throw new AssertionError(e);
- }
- }
-
- private void addTypeIndex(ServiceManager serviceManager, RegistryFutureTask registryFutureTask) {
- if (serviceManager == null) throw new NullPointerException("serviceManager is null");
- if (registryFutureTask == null) throw new NullPointerException("serviceManagerFuture is null");
-
- Set allTypes = new LinkedHashSet();
- for (Iterator iterator = serviceManager.getServiceTypes().iterator(); iterator.hasNext();) {
- Class serviceType = (Class) iterator.next();
-
- if (serviceType.isArray()) {
- throw new IllegalArgumentException("Service is an array: serviceName=" + serviceManager.getServiceName() +
- ", serviceType=" + serviceManager.getServiceTypes());
- }
-
- allTypes.add(serviceType);
- allTypes.addAll(getAllSuperClasses(serviceType));
- allTypes.addAll(getAllInterfaces(serviceType));
- }
-
- synchronized (serviceManagers) {
- for (Iterator iterator = allTypes.iterator(); iterator.hasNext();) {
- Class type = (Class) iterator.next();
- Set futureServiceManagers = (Set) serviceManagersByType.get(type);
- if (futureServiceManagers == null) {
- futureServiceManagers = new TreeSet();
- serviceManagersByType.put(type, futureServiceManagers);
- }
- futureServiceManagers.add(registryFutureTask);
- }
- }
- }
-
- private void removeTypeIndex(RegistryFutureTask registryFutureTask) {
- if (registryFutureTask == null) throw new NullPointerException("serviceManagerFuture is null");
- synchronized (serviceManagers) {
- for (Iterator iterator = serviceManagersByType.entrySet().iterator(); iterator.hasNext();) {
- Map.Entry entry = (Map.Entry) iterator.next();
- Set serviceManagers = (Set) entry.getValue();
- serviceManagers.remove(registryFutureTask);
- if (serviceManagers.isEmpty()) {
- iterator.remove();
- }
- }
- }
- }
-
- private static Set getAllSuperClasses(Class clazz) {
- Set allSuperClasses = new LinkedHashSet();
- for (Class superClass = clazz.getSuperclass(); superClass != null; superClass = superClass.getSuperclass()) {
- allSuperClasses.add(superClass);
- }
- return allSuperClasses;
- }
-
- private static Set getAllInterfaces(Class clazz) {
- Set allInterfaces = new LinkedHashSet();
- LinkedList stack = new LinkedList();
- stack.addAll(Arrays.asList(clazz.getInterfaces()));
- while (!stack.isEmpty()) {
- Class intf = (Class) stack.removeFirst();
- if (!allInterfaces.contains(intf)) {
- allInterfaces.add(intf);
- stack.addAll(Arrays.asList(intf.getInterfaces()));
- }
- }
- return allInterfaces;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/ServiceMonitorBroadcaster.java b/kernel/src/java/org/xbean/kernel/standard/ServiceMonitorBroadcaster.java
deleted file mode 100644
index ac7f5b4a..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/ServiceMonitorBroadcaster.java
+++ /dev/null
@@ -1,325 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.xbean.kernel.ServiceMonitor;
-import org.xbean.kernel.KernelMonitor;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.ServiceEvent;
-import org.xbean.kernel.KernelErrorsError;
-
-/**
- * The ServiceMonitorBroadcaster broadcasts kernel events to registered service monitors.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceMonitorBroadcaster implements ServiceMonitor {
- /**
- * The monitors for service events.
- */
- private final Map serviceMonitors = new LinkedHashMap();
-
- /**
- * The monitor we notify when we get an exception from a service monitor.
- */
- private final KernelMonitor kernelMonitor;
-
- /**
- * Creates a ServiceMonitorBroadcaster that notifies the specified kernel monitor when an error occurs while
- * notifying the registered service monitors.
- *
- * @param kernelMonitor the monitor to notify when an error occurs while notifying the registered service monitors
- */
- public ServiceMonitorBroadcaster(KernelMonitor kernelMonitor) {
- if (kernelMonitor == null) throw new NullPointerException("kernelMonitor is null");
- this.kernelMonitor = kernelMonitor;
- }
-
- /**
- * Adds a service monitor for a specific service, or if the specified service name is null, a global monitor.
- *
- * Note: the order in which service monitors are notified is not specified.
- *
- * @param serviceMonitor the service monitor to add
- * @param serviceName the unique name of the service to monitor or null to monitor all services
- */
- public void addServiceMonitor(ServiceMonitor serviceMonitor, ServiceName serviceName) {
- if (serviceMonitor == null) throw new NullPointerException("serviceMonitor is null");
- synchronized (serviceMonitors) {
- Set monitors = (Set) serviceMonitors.get(serviceName);
- if (monitors == null) {
- monitors = new LinkedHashSet();
- serviceMonitors.put(serviceName, monitors);
- }
- monitors.add(serviceMonitor);
- }
- }
-
- /**
- * Removes a service monitor.
- *
- * @param serviceMonitor the service monitor to remove
- */
- public void removeServiceMonitor(ServiceMonitor serviceMonitor) {
- if (serviceMonitor == null) throw new NullPointerException("serviceMonitor is null");
- synchronized (serviceMonitors) {
- for (Iterator iterator = serviceMonitors.values().iterator(); iterator.hasNext();) {
- Set monitors = (Set) iterator.next();
- monitors.remove(serviceMonitor);
- if (monitors.isEmpty()) {
- iterator.remove();
- }
- }
- }
- }
-
- /**
- * Gets the service monitors registered to recieve events for the specified service. This will include all global
- * monitors and service specific monitors.
- *
- * @param serviceName the name of the service
- * @return the monitors registerd to recieve events for the specified service
- */
- private Set getServiceMonitors(ServiceName serviceName) {
- synchronized (serviceMonitors) {
- Set monitors = new LinkedHashSet();
- Set globalMonitors = (Set) serviceMonitors.get(null);
- if (globalMonitors != null) {
- monitors.addAll(globalMonitors);
- }
- Set specificMonitors = (Set) serviceMonitors.get(serviceName);
- if (specificMonitors != null) {
- monitors.addAll(specificMonitors);
- }
- return monitors;
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceRegistered(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceRegistered(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStarting(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceStarting(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceWaitingToStart(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceWaitingToStart(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStartError(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceStartError(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceRunning(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceRunning(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopping(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceStopping(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceWaitingToStop(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceWaitingToStop(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopError(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceStopError(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceStopped(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceStopped(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void serviceUnregistered(ServiceEvent serviceEvent) {
- List errors = new ArrayList();
- Set serviceMonitors = getServiceMonitors(serviceEvent.getServiceName());
- for (Iterator iterator = serviceMonitors.iterator(); iterator.hasNext();) {
- ServiceMonitor serviceMonitor = (ServiceMonitor) iterator.next();
- try {
- serviceMonitor.serviceUnregistered(serviceEvent);
- } catch (Throwable e) {
- errors.addAll(fireServiceNotificationError(serviceMonitor, serviceEvent, e));
- }
- }
- if (!errors.isEmpty()) {
- throw new KernelErrorsError(errors);
- }
- }
-
- private List fireServiceNotificationError(ServiceMonitor serviceMonitor, ServiceEvent serviceEvent, Throwable throwable) {
- try {
- kernelMonitor.serviceNotificationError(serviceMonitor, serviceEvent, throwable);
- } catch (RuntimeException ignored) {
- // ignore - we did our best to notify the world
- } catch (KernelErrorsError e) {
- return e.getErrors();
- } catch (Error e) {
- return Collections.singletonList(e);
- }
- return Collections.EMPTY_LIST;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/StandardKernel.java b/kernel/src/java/org/xbean/kernel/standard/StandardKernel.java
deleted file mode 100644
index 4105bf51..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/StandardKernel.java
+++ /dev/null
@@ -1,495 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Iterator;
-
-import edu.emory.mathcs.backport.java.util.concurrent.Executor;
-import edu.emory.mathcs.backport.java.util.concurrent.Executors;
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Condition;
-import org.xbean.kernel.IllegalServiceStateException;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.KernelErrorsError;
-import org.xbean.kernel.KernelMonitor;
-import org.xbean.kernel.ServiceAlreadyExistsException;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceMonitor;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.ServiceNotFoundException;
-import org.xbean.kernel.ServiceRegistrationException;
-import org.xbean.kernel.ServiceState;
-import org.xbean.kernel.StartStrategies;
-import org.xbean.kernel.StartStrategy;
-import org.xbean.kernel.StopStrategies;
-import org.xbean.kernel.StopStrategy;
-import org.xbean.kernel.UnregisterServiceException;
-import org.xbean.kernel.UnsatisfiedConditionsException;
-import org.xbean.kernel.KernelFactory;
-
-/**
- * The standard kernel implementation.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StandardKernel implements Kernel {
- /**
- * The unique name of this kernel.
- */
- private final String kernelName;
-
- /**
- * The registered service managers.
- */
- private final ServiceManagerRegistry serviceManagerRegistry;
-
- /**
- * Tracks and broadcasts kernel evnents to the registered listeners.
- */
- private final KernelMonitorBroadcaster kernelMonitor = new KernelMonitorBroadcaster();
-
- /**
- * This monitor broadcasts events to the listeners registered for service.
- */
- private final ServiceMonitorBroadcaster serviceMonitor = new ServiceMonitorBroadcaster(kernelMonitor);
-
- /**
- * If true, the kernel is still running.
- */
- private boolean running = true;
-
- /**
- * Lock that should be acquired before accessing the running boolean flag.
- */
- private final Lock destroyLock = new ReentrantLock();
-
- /**
- * The condition that is notified when the kernel has been destroyed.
- */
- private final Condition destroyCondition = destroyLock.newCondition();
-
- /**
- * Creates the service managers with handle service lifecycle.
- */
- private ServiceManagerFactory serviceManagerFactory;
-
- /**
- * Creates a kernel using the specified name.
- *
- * @param kernelName the unique name of this kernel
- */
- public StandardKernel(String kernelName) {
- this(kernelName, Executors.newCachedThreadPool(), 30, TimeUnit.SECONDS);
- }
-
- /**
- * Creates a kernel using the specified name.
- *
- * @param kernelName the unique name of this kernel
- * @param serviceExecutor the executor to use for asynchronous service operations
- * @param timeoutDuration the maximum duration to wait for a service event to complete
- * @param timeoutUnits the unit of measure for the timeoutDuration
- */
- public StandardKernel(String kernelName, Executor serviceExecutor, long timeoutDuration, TimeUnit timeoutUnits) {
- if (kernelName == null) throw new NullPointerException("kernelName is null");
- if (kernelName.length() ==0) throw new IllegalArgumentException("kernelName must be atleast one character long");
- if (serviceExecutor == null) throw new NullPointerException("serviceExecutor is null");
- if (timeoutUnits == null) throw new NullPointerException("timeoutUnits is null");
-
- this.kernelName = kernelName;
- serviceManagerFactory = new ServiceManagerFactory(this, serviceMonitor, serviceExecutor, timeoutDuration, timeoutUnits);
- serviceManagerRegistry = new ServiceManagerRegistry(serviceManagerFactory);
- }
-
- /**
- * {@inheritDoc}
- */
- public void destroy() throws KernelErrorsError {
- destroyLock.lock();
- try {
- // if we are already stopped simply return
- if (!running) {
- return;
- }
- running = false;
- } finally {
- destroyLock.unlock();
- }
-
- // destroy all services
- serviceManagerRegistry.destroy();
-
- // remove this kernel from the kernel factory registry
- KernelFactory.destroyInstance(this);
-
- // notify threads waiting for destroy to complete
- destroyLock.lock();
- try {
- destroyCondition.signalAll();
- } finally {
- destroyLock.unlock();
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void waitForDestruction() {
- destroyLock.lock();
- try {
- // if we are already stopped simply return
- if (!running) {
- return;
- }
-
- // wait until destroy completes
- destroyCondition.awaitUninterruptibly();
- } finally {
- destroyLock.unlock();
- }
-
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isRunning() {
- destroyLock.lock();
- try {
- return running;
- } finally {
- destroyLock.unlock();
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public String getKernelName() {
- return kernelName;
- }
-
- /**
- * {@inheritDoc}
- */
- public void registerService(ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader) throws ServiceAlreadyExistsException, ServiceRegistrationException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (serviceFactory == null) throw new NullPointerException("serviceFactory is null");
- if (classLoader == null) throw new NullPointerException("classLoader is null");
- if (!isRunning()) {
- throw new ServiceRegistrationException(serviceName, new IllegalStateException("Kernel is destroyed"));
- }
-
- serviceManagerRegistry.registerService(serviceName, serviceFactory, classLoader);
- }
-
- /**
- * {@inheritDoc}
- */
- public void unregisterService(ServiceName serviceName) throws ServiceNotFoundException, ServiceRegistrationException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- unregisterService(serviceName, StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * {@inheritDoc}
- */
- public void unregisterService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, ServiceRegistrationException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (stopStrategy == null) throw new NullPointerException("stopStrategy is null");
- if (!isRunning()) {
- return;
- }
-
- serviceManagerRegistry.unregisterService(serviceName, stopStrategy);
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isRegistered(ServiceName serviceName) {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (!isRunning()) {
- return false;
- }
-
- return serviceManagerRegistry.isRegistered(serviceName);
- }
-
- /**
- * {@inheritDoc}
- */
- public ServiceState getServiceState(ServiceName serviceName) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- return serviceManager.getState();
- }
-
- /**
- * {@inheritDoc}
- */
- public long getServiceStartTime(ServiceName serviceName) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- return serviceManager.getStartTime();
- }
-
- /**
- * {@inheritDoc}
- */
- public void startService(ServiceName serviceName) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- startService(serviceName, false, StartStrategies.SYNCHRONOUS);
- }
-
- /**
- * {@inheritDoc}
- */
- public void startService(ServiceName serviceName, StartStrategy startStrategy) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (startStrategy == null) throw new NullPointerException("startStrategy is null");
- startService(serviceName, false, startStrategy);
- }
-
- /**
- * {@inheritDoc}
- */
- public void startServiceRecursive(ServiceName serviceName) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- startService(serviceName, true, StartStrategies.SYNCHRONOUS);
- }
-
- /**
- * {@inheritDoc}
- */
- public void startServiceRecursive(ServiceName serviceName, StartStrategy startStrategy) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (startStrategy == null) throw new NullPointerException("startStrategy is null");
- startService(serviceName, true, startStrategy);
- }
-
- private void startService(ServiceName serviceName, boolean recursive, StartStrategy startStrategy) throws Exception {
- if (startStrategy == null) throw new NullPointerException("startStrategy is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- try {
- serviceManager.start(recursive, startStrategy);
- } catch (UnregisterServiceException e) {
- try {
- unregisterService(serviceName, StopStrategies.FORCE);
- } catch (ServiceNotFoundException ignored) {
- // that is weird, but what ever
- } catch (ServiceRegistrationException ignored) {
- // we are alredy throwing an exception so ignore this one
- }
- Throwable cause = e.getCause();
- if (cause instanceof Exception) {
- throw (Exception) cause;
- } else if (cause instanceof Error) {
- throw (Error) cause;
- } else {
- throw new AssertionError(cause);
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void stopService(ServiceName serviceName) throws ServiceNotFoundException, UnsatisfiedConditionsException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- stopService(serviceName, StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * {@inheritDoc}
- */
- public void stopService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, UnsatisfiedConditionsException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (stopStrategy == null) throw new NullPointerException("stopStrategy is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- serviceManager.stop(stopStrategy);
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isServiceEnabled(ServiceName serviceName) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- ServiceFactory serviceFactory = serviceManager.getServiceFactory();
- return serviceFactory.isEnabled();
- }
-
- /**
- * {@inheritDoc}
- */
- public void setServiceEnabled(ServiceName serviceName, boolean enabled) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- ServiceFactory serviceFactory = serviceManager.getServiceFactory();
- serviceFactory.setEnabled(enabled);
- }
-
- /**
- * {@inheritDoc}
- */
- public Object getService(ServiceName serviceName) throws ServiceNotFoundException, IllegalArgumentException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- return serviceManager.getService();
- }
-
- /**
- * {@inheritDoc}
- */
- public Object getService(Class type) {
- if (type == null) throw new NullPointerException("type is null");
- if (!isRunning()) {
- return null;
- }
-
- Object service = serviceManagerRegistry.getService(type);
- return service;
- }
-
- /**
- * {@inheritDoc}
- */
- public List getServices(Class type) {
- if (type == null) throw new NullPointerException("type is null");
- if (!isRunning()) {
- return null;
- }
-
- List services = serviceManagerRegistry.getServices(type);
- return services;
- }
-
- /**
- * {@inheritDoc}
- */
- public ServiceFactory getServiceFactory(ServiceName serviceName) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- return serviceManager.getServiceFactory();
- }
-
- /**
- * {@inheritDoc}
- */
- public ServiceFactory getServiceFactory(Class type) {
- if (type == null) throw new NullPointerException("type is null");
- if (!isRunning()) {
- return null;
- }
-
- ServiceManager serviceManager = serviceManagerRegistry.getServiceManager(type);
- return serviceManager.getServiceFactory();
- }
-
- /**
- * {@inheritDoc}
- */
- public List getServiceFactories(Class type) {
- if (type == null) throw new NullPointerException("type is null");
- if (!isRunning()) {
- return null;
- }
-
- List serviceManagers = serviceManagerRegistry.getServiceManagers(type);
- List serviceFactories = new ArrayList(serviceManagers.size());
- for (Iterator iterator = serviceManagers.iterator(); iterator.hasNext();) {
- ServiceManager serviceManager = (ServiceManager) iterator.next();
- serviceFactories.add(serviceManager.getServiceFactory());
- }
- return serviceFactories;
- }
-
- /**
- * {@inheritDoc}
- */
- public ClassLoader getClassLoaderFor(ServiceName serviceName) throws ServiceNotFoundException {
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- ServiceManager serviceManager = getServiceManager(serviceName);
- return serviceManager.getClassLoader();
- }
-
- private ServiceManager getServiceManager(ServiceName serviceName) throws ServiceNotFoundException {
- if (!isRunning()) {
- throw new ServiceNotFoundException(serviceName);
- }
-
- ServiceManager serviceManager = serviceManagerRegistry.getServiceManager(serviceName);
- return serviceManager;
- }
-
- /**
- * {@inheritDoc}
- */
- public void addKernelMonitor(KernelMonitor kernelMonitor) {
- if (kernelMonitor == null) throw new NullPointerException("kernelMonitor is null");
- if (!isRunning()) {
- throw new IllegalStateException("Kernel is stopped");
- }
- this.kernelMonitor.addKernelMonitor(kernelMonitor);
- }
-
- /**
- * {@inheritDoc}
- */
- public void removeKernelMonitor(KernelMonitor kernelMonitor) {
- if (kernelMonitor == null) throw new NullPointerException("kernelMonitor is null");
- this.kernelMonitor.removeKernelMonitor(kernelMonitor);
- }
-
- /**
- * {@inheritDoc}
- */
- public void addServiceMonitor(ServiceMonitor serviceMonitor) {
- if (serviceMonitor == null) throw new NullPointerException("serviceMonitor is null");
- if (!isRunning()) {
- throw new IllegalStateException("Kernel is stopped");
- }
- addServiceMonitor(serviceMonitor, null);
- }
-
- /**
- * {@inheritDoc}
- */
- public void addServiceMonitor(ServiceMonitor serviceMonitor, ServiceName serviceName) {
- if (serviceMonitor == null) throw new NullPointerException("serviceMonitor is null");
- if (serviceName == null) throw new NullPointerException("serviceName is null");
- if (!isRunning()) {
- throw new IllegalStateException("Kernel is stopped");
- }
- this.serviceMonitor.addServiceMonitor(serviceMonitor, serviceName);
- }
-
- /**
- * {@inheritDoc}
- */
- public void removeServiceMonitor(ServiceMonitor serviceMonitor) {
- if (serviceMonitor == null) throw new NullPointerException("serviceMonitor is null");
- this.serviceMonitor.removeServiceMonitor(serviceMonitor);
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/StandardKernelFactory.java b/kernel/src/java/org/xbean/kernel/standard/StandardKernelFactory.java
deleted file mode 100644
index 56344aa5..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/StandardKernelFactory.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.KernelFactory;
-
-/**
- * The kernel factory for StandardKernel instances.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StandardKernelFactory extends KernelFactory {
- /**
- * {@inheritDoc}
- */
- protected Kernel createKernelInternal(String name) {
- if (name == null) throw new NullPointerException("name is null");
- return new StandardKernel(name);
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/StandardServiceConditionContext.java b/kernel/src/java/org/xbean/kernel/standard/StandardServiceConditionContext.java
deleted file mode 100644
index 16acae8e..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/StandardServiceConditionContext.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Condition;
-import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.ServiceConditionContext;
-import org.xbean.kernel.ServiceName;
-
-/**
- * This is the service context used by the service manager.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StandardServiceConditionContext implements ServiceConditionContext {
- /**
- * The kernel in which the service is registered.
- */
- private final Kernel kernel;
-
- /**
- * The unique name of the service in the kernel.
- */
- private final ServiceName serviceName;
-
- /**
- * The class loader for the service.
- */
- private final ClassLoader classLoader;
-
- /**
- * The lock that must be acquired before signaling the condition.
- */
- private final Lock lock;
-
- /**
- * The condition to signal when the {@link #setSatisfied()} method is called.
- */
- private final Condition condition;
-
- /**
- * Has this condition been satisfied? Once satisfied a condition is always considered satisfied.
- */
- private boolean satisfied = false;
-
- /**
- * Creates a service context for the specified service.
- *
- * @param kernel the kernel in which the service is registered
- * @param serviceName the name of the service
- * @param classLoader the class loader for the service
- * @param lock the lock for the service manager
- * @param condition the condition that should be notified when the {@link #setSatisfied()} method is called
- */
- public StandardServiceConditionContext(Kernel kernel, ServiceName serviceName, ClassLoader classLoader, Lock lock, Condition condition) {
- this.kernel = kernel;
- this.serviceName = serviceName;
- this.classLoader = classLoader;
- this.lock = lock;
- this.condition = condition;
- }
-
- /**
- * {@inheritDoc}
- */
- public Kernel getKernel() {
- return kernel;
- }
-
- /**
- * {@inheritDoc}
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * {@inheritDoc}
- */
- public ClassLoader getClassLoader() {
- return classLoader;
- }
-
- /**
- * Gets the satisfied status of this condition. Once satisfied a condition is considered satisfied until destroyed
- * and reinitialized. The ServiceManager uses the StandardServiceConditionContext to track the status of conditions
- * so it will call setSatisfied() when the condition returns true from isSatisfied().
- *
- * @return satisfied status of this condition
- */
- public boolean isSatisfied() {
- return satisfied;
- }
-
- /**
- * {@inheritDoc}
- */
- public void setSatisfied() {
- lock.lock();
- try {
- if (!satisfied) {
- satisfied = true;
- condition.signalAll();
- }
- } finally {
- lock.unlock();
- }
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/StandardServiceContext.java b/kernel/src/java/org/xbean/kernel/standard/StandardServiceContext.java
deleted file mode 100644
index 140b1bd2..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/StandardServiceContext.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.ServiceContext;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceName;
-
-/**
- * The standard service context implementation. This is passed to the service factory in the
- * {@link ServiceFactory#createService(ServiceContext)} and {@link ServiceFactory#destroyService(ServiceContext)}
- * methods.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StandardServiceContext implements ServiceContext {
- private final Kernel kernel;
- private final ServiceName serviceName;
- private final ClassLoader classLoader;
-
- /**
- * Creates the standard service context implementation.
- *
- * @param kernel the kernel in which the service is registered
- * @param serviceName the name of the service
- * @param classLoader the class loader for the service
- */
- public StandardServiceContext(Kernel kernel, ServiceName serviceName, ClassLoader classLoader) {
- this.kernel = kernel;
- this.serviceName = serviceName;
- this.classLoader = classLoader;
- }
-
- /**
- * {@inheritDoc}
- */
- public Kernel getKernel() {
- return kernel;
- }
-
- /**
- * {@inheritDoc}
- */
- public ServiceName getServiceName() {
- return serviceName;
- }
-
- /**
- * {@inheritDoc}
- */
- public ClassLoader getClassLoader() {
- return classLoader;
- }
-}
diff --git a/kernel/src/java/org/xbean/kernel/standard/package.html b/kernel/src/java/org/xbean/kernel/standard/package.html
deleted file mode 100644
index eac9184c..00000000
--- a/kernel/src/java/org/xbean/kernel/standard/package.html
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-Provides the standard kernel implementation. The classes contined in this package should be considered private
-implementation details of XBean, and should not be used directly.
-
-
\ No newline at end of file
diff --git a/kernel/src/test/org/xbean/kernel/KernelMonitorBroadcasterTest.java b/kernel/src/test/org/xbean/kernel/KernelMonitorBroadcasterTest.java
deleted file mode 100644
index 2b97c381..00000000
--- a/kernel/src/test/org/xbean/kernel/KernelMonitorBroadcasterTest.java
+++ /dev/null
@@ -1,175 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import junit.framework.TestCase;
-import org.xbean.kernel.standard.StandardKernel;
-import org.xbean.kernel.standard.KernelMonitorBroadcaster;
-
-/**
- * Tests the KernelMonitorBroadcaster.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class KernelMonitorBroadcasterTest extends TestCase {
- private static final ServiceMonitor SERVICE_MONITOR = new NullServiceMonitor();
- private static final ServiceEvent SERVICE_EVENT = new ServiceEvent(0,
- new StandardKernel("test"),
- new StringServiceName("service-name"),
- new StaticServiceFactory(new Object()),
- ClassLoader.getSystemClassLoader(),
- null,
- null,
- null);
- private static final Throwable THROWABLE = new Throwable("test throwable");
-
- private static final int MONITOR_COUNT = 4;
- private MockKernelMonitor[] kernelMonitors = new MockKernelMonitor[MONITOR_COUNT];
- private KernelMonitorBroadcaster kernelMonitorBroadcaster = new KernelMonitorBroadcaster();
-
- protected void setUp() throws Exception {
- super.setUp();
- for (int i = 0; i < kernelMonitors.length; i++) {
- kernelMonitors[i] = new MockKernelMonitor("monitor-" + i);
- kernelMonitorBroadcaster.addKernelMonitor(kernelMonitors[i]);
- }
- }
-
- /**
- * Test that events are fired to all registered monitors.
- * Strategy:
- *
- * Add the monitors
- *
- * Fire an event
- *
- * Verify that the event was called on each monitor
- *
- */
- public void testFireEvent() {
- kernelMonitorBroadcaster.serviceNotificationError(SERVICE_MONITOR, SERVICE_EVENT, THROWABLE);
- assertNotificationCorrect();
- }
-
- private void assertNotificationCorrect() {
- for (int i = 0; i < kernelMonitors.length; i++) {
- MockKernelMonitor kernelMonitor = kernelMonitors[i];
- assertTrue(kernelMonitor.wasNotificationCalled());
- }
- }
-
- /**
- * Test that if a monitor is added more then once it only recieves the event once.
- * Strategy:
- *
- * Add a monitor several times
- *
- * Fire an event
- *
- * Verify that the monitor was only notified once
- *
- */
- public void testDoubleAdd() {
- kernelMonitorBroadcaster.addKernelMonitor(kernelMonitors[1]);
- kernelMonitorBroadcaster.addKernelMonitor(kernelMonitors[1]);
- kernelMonitorBroadcaster.addKernelMonitor(kernelMonitors[1]);
-
- kernelMonitorBroadcaster.serviceNotificationError(SERVICE_MONITOR, SERVICE_EVENT, THROWABLE);
-
- // note the mock monitor asserts that it is only called once before requreing a reset
- assertNotificationCorrect();
- }
-
- /**
- * Test that events are not fired to a monitor that has been removed.
- * Strategy:
- *
- * Add the monitors
- *
- * Fire an event
- *
- * Verify that the event was called on each monitor
- *
- * Removes a monitors
- *
- * Fire an event
- *
- * Verify that the event was called on each monitor except the removed monitor
- *
- */
- public void testRemove() {
- kernelMonitorBroadcaster.serviceNotificationError(SERVICE_MONITOR, SERVICE_EVENT, THROWABLE);
-
- assertNotificationCorrect();
-
- for (int i = 0; i < kernelMonitors.length; i++) {
- MockKernelMonitor kernelMonitor = kernelMonitors[i];
- kernelMonitor.rest();
- }
-
- kernelMonitorBroadcaster.removeKernelMonitor(kernelMonitors[1]);
-
- kernelMonitorBroadcaster.serviceNotificationError(SERVICE_MONITOR, SERVICE_EVENT, THROWABLE);
-
- for (int i = 0; i < kernelMonitors.length; i++) {
- MockKernelMonitor kernelMonitor = kernelMonitors[i];
- if (i == 1) {
- assertFalse(kernelMonitor.wasNotificationCalled());
- } else {
- assertTrue(kernelMonitor.wasNotificationCalled());
- }
- }
- }
-
- /**
- * Tests that no exceptions are thrown if an attempt is made to remove a monitor not registered.
- */
- public void testRemoveUnassociated() {
- kernelMonitorBroadcaster.removeKernelMonitor(new MockKernelMonitor("unassociated monitor"));
- }
-
- private static class MockKernelMonitor implements KernelMonitor {
- private final String name;
- private boolean notificationCalled = false;
-
- private MockKernelMonitor(String name) {
- this.name = name;
- }
-
- public String toString() {
- return name;
- }
-
- private void rest() {
- notificationCalled = false;
- }
-
- public boolean wasNotificationCalled() {
- return notificationCalled;
- }
-
- public void serviceNotificationError(ServiceMonitor serviceMonitor, ServiceEvent serviceEvent, Throwable throwable) {
- assertFalse(notificationCalled);
- notificationCalled = true;
- assertSame(SERVICE_MONITOR, serviceMonitor);
- assertSame(SERVICE_EVENT, serviceEvent);
- assertSame(THROWABLE, throwable);
- }
- }
-}
\ No newline at end of file
diff --git a/kernel/src/test/org/xbean/kernel/ServiceStateTest.java b/kernel/src/test/org/xbean/kernel/ServiceStateTest.java
deleted file mode 100644
index dd66aa4d..00000000
--- a/kernel/src/test/org/xbean/kernel/ServiceStateTest.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.rmi.MarshalledObject;
-
-import junit.framework.TestCase;
-
-/**
- * Tests that the ServiceState constants are consistent.
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceStateTest extends TestCase {
- /**
- * Tests that the constances are .equals to them selves and no other constants.
- */
- public void testEquals() {
- assertTrue(ServiceState.STARTING.equals(ServiceState.STARTING));
- assertFalse(ServiceState.STARTING.equals(ServiceState.RUNNING));
- assertFalse(ServiceState.STARTING.equals(ServiceState.STOPPING));
- assertFalse(ServiceState.STARTING.equals(ServiceState.STOPPED));
-
- assertFalse(ServiceState.RUNNING.equals(ServiceState.STARTING));
- assertTrue(ServiceState.RUNNING.equals(ServiceState.RUNNING));
- assertFalse(ServiceState.RUNNING.equals(ServiceState.STOPPING));
- assertFalse(ServiceState.RUNNING.equals(ServiceState.STOPPED));
-
- assertFalse(ServiceState.STOPPING.equals(ServiceState.STARTING));
- assertFalse(ServiceState.STOPPING.equals(ServiceState.RUNNING));
- assertTrue(ServiceState.STOPPING.equals(ServiceState.STOPPING));
- assertFalse(ServiceState.STOPPING.equals(ServiceState.STOPPED));
-
- assertFalse(ServiceState.STOPPED.equals(ServiceState.STARTING));
- assertFalse(ServiceState.STOPPED.equals(ServiceState.RUNNING));
- assertFalse(ServiceState.STOPPED.equals(ServiceState.STOPPING));
- assertTrue(ServiceState.STOPPED.equals(ServiceState.STOPPED));
- }
-
- /**
- * Tests that the constants create a hashCode that is equal to their own hashCode an no other constants hashCode.
- */
- public void testHashCode() {
- assertTrue(ServiceState.STARTING.hashCode() == ServiceState.STARTING.hashCode());
- assertFalse(ServiceState.STARTING.hashCode() == ServiceState.RUNNING.hashCode());
- assertFalse(ServiceState.STARTING.hashCode() == ServiceState.STOPPING.hashCode());
- assertFalse(ServiceState.STARTING.hashCode() == ServiceState.STOPPED.hashCode());
-
- assertFalse(ServiceState.RUNNING.hashCode() == ServiceState.STARTING.hashCode());
- assertTrue(ServiceState.RUNNING.hashCode() == ServiceState.RUNNING.hashCode());
- assertFalse(ServiceState.RUNNING.hashCode() == ServiceState.STOPPING.hashCode());
- assertFalse(ServiceState.RUNNING.hashCode() == ServiceState.STOPPED.hashCode());
-
- assertFalse(ServiceState.STOPPING.hashCode() == ServiceState.STARTING.hashCode());
- assertFalse(ServiceState.STOPPING.hashCode() == ServiceState.RUNNING.hashCode());
- assertTrue(ServiceState.STOPPING.hashCode() == ServiceState.STOPPING.hashCode());
- assertFalse(ServiceState.STOPPING.hashCode() == ServiceState.STOPPED.hashCode());
-
- assertFalse(ServiceState.STOPPED.hashCode() == ServiceState.STARTING.hashCode());
- assertFalse(ServiceState.STOPPED.hashCode() == ServiceState.RUNNING.hashCode());
- assertFalse(ServiceState.STOPPED.hashCode() == ServiceState.STOPPING.hashCode());
- assertTrue(ServiceState.STOPPED.hashCode() == ServiceState.STOPPED.hashCode());
- }
-
- /**
- * Tests that getServiceState returns the same constant as the constant from which the index was gotten, and that
- * getServiceState throws an exception if an attempt is make to get an unknown constant.
- */
- public void testGetServiceState() {
- assertSame(ServiceState.STARTING, ServiceState.getServiceState(ServiceState.STARTING.getIndex()));
- assertSame(ServiceState.RUNNING, ServiceState.getServiceState(ServiceState.RUNNING.getIndex()));
- assertSame(ServiceState.STOPPING, ServiceState.getServiceState(ServiceState.STOPPING.getIndex()));
- assertSame(ServiceState.STOPPED, ServiceState.getServiceState(ServiceState.STOPPED.getIndex()));
-
- try {
- ServiceState.getServiceState(ServiceState.STOPPED.getIndex() + 1);
- fail("ServiceState.getServiceState(ServiceState.STOPPED.getIndex() + 1) should have thrown and exception");
- } catch (IllegalArgumentException expected) {
- // expected
- }
-
- try {
- ServiceState.getServiceState(ServiceState.STARTING.getIndex() - 1);
- fail("ServiceState.getServiceState(ServiceState.STARTING.getIndex() - 1) should have thrown and exception");
- } catch (IllegalArgumentException expected) {
- // expected
- }
- }
-
- /**
- * Tests that parseServiceState returns the same state when called with getName on a state, that it throws an exception
- * for an unknown state and that the parsing is done using a case insensitive match.
- */
- public void testParseServiceState() {
- assertSame(ServiceState.STARTING, ServiceState.parseServiceState(ServiceState.STARTING.getName()));
- assertSame(ServiceState.RUNNING, ServiceState.parseServiceState(ServiceState.RUNNING.getName()));
- assertSame(ServiceState.STOPPING, ServiceState.parseServiceState(ServiceState.STOPPING.getName()));
- assertSame(ServiceState.STOPPED, ServiceState.parseServiceState(ServiceState.STOPPED.getName()));
-
- try {
- ServiceState.parseServiceState("donkey");
- fail("ServiceState.parseServiceState(\"donkey\") should have thrown and exception");
- } catch (IllegalArgumentException expected) {
- // expected
- }
-
- assertSame(ServiceState.STARTING, ServiceState.parseServiceState("StARting"));
- assertSame(ServiceState.RUNNING, ServiceState.parseServiceState("running"));
- assertSame(ServiceState.STOPPING, ServiceState.parseServiceState("stoPPing"));
- assertSame(ServiceState.STOPPED, ServiceState.parseServiceState("StoppeD"));
- }
-
- /**
- * Tests that when a state is serialized an deserialized it returns the same state object. This test shoudl assure
- * that there is only one state instance with a specific index in existant at one time.
- * @throws Exception if a problem occurs
- */
- public void testSerialization() throws Exception {
- assertSame(ServiceState.STARTING, copyServiceState(ServiceState.STARTING));
- assertSame(ServiceState.RUNNING, copyServiceState(ServiceState.RUNNING));
- assertSame(ServiceState.STOPPING, copyServiceState(ServiceState.STOPPING));
- assertSame(ServiceState.STOPPED, copyServiceState(ServiceState.STOPPED));
- }
-
- private ServiceState copyServiceState(ServiceState original) throws Exception {
- MarshalledObject marshalledObject = new MarshalledObject(original);
- return (ServiceState) marshalledObject.get();
- }
-}
diff --git a/kernel/src/test/org/xbean/kernel/StaticServiceFactoryTest.java b/kernel/src/test/org/xbean/kernel/StaticServiceFactoryTest.java
deleted file mode 100644
index 29d75a8f..00000000
--- a/kernel/src/test/org/xbean/kernel/StaticServiceFactoryTest.java
+++ /dev/null
@@ -1,179 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import java.util.Set;
-import java.util.TreeSet;
-
-import junit.framework.TestCase;
-
-/**
- * Tests the StaticServiceFactory.
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StaticServiceFactoryTest extends TestCase {
- private static final Object SERVICE = new TreeSet();
- private static final ServiceContext SERVICE_CONTEXT = new MockServiceContext();
- private static final ServiceCondition START_CONDITION = new MockStartCondition();
- private static final ServiceCondition STOP_CONDITION = new MockStopCondition();
-
- /**
- * Tests that the constructor works when called with an Object and fails when called with null.
- */
- public void testConstructor() {
- new StaticServiceFactory(SERVICE);
- try {
- new StaticServiceFactory(null);
- fail("new StaticServiceFactory(null) should have thrown a NullPointerException");
- } catch (NullPointerException expected) {
- }
- }
-
- /**
- * Tests that create service returns the same object that was passed to the constructor.
- */
- public void testCreateService() {
- assertSame(SERVICE, new StaticServiceFactory(SERVICE).createService(SERVICE_CONTEXT));
- }
-
- /**
- * Tests that the service factory is not restartable.
- */
- public void testIsRestartable() {
- assertEquals(false, new StaticServiceFactory(SERVICE).isRestartable());
- }
-
- /**
- * Tests that geting and setting the enalbe flag.
- */
- public void testEnabled() {
- StaticServiceFactory serviceFactory = new StaticServiceFactory(SERVICE);
- assertEquals(true, serviceFactory.isEnabled());
- serviceFactory.setEnabled(false);
- assertEquals(false, serviceFactory.isEnabled());
- serviceFactory.setEnabled(true);
- assertEquals(true, serviceFactory.isEnabled());
- }
-
- /**
- * Tests getting and setting start and stop conditions.
- */
- public void testConditions() {
- StaticServiceFactory serviceFactory = new StaticServiceFactory(SERVICE);
-
- // get the dependency set
- Set dependencies = serviceFactory.getStartConditions();
- assertNotNull(dependencies);
- // it should be initially empty
- assertTrue(dependencies.isEmpty());
-
- serviceFactory.addStartCondition(START_CONDITION);
- // old dependency set should still be empty... it is a snapshot
- assertTrue(dependencies.isEmpty());
-
- // get a new dependency set
- dependencies = serviceFactory.getStartConditions();
- assertNotNull(dependencies);
- // should have our dependency in it
- assertEquals(1, dependencies.size());
- assertTrue(dependencies.contains(START_CONDITION));
-
- try {
- dependencies.clear();
- fail("dependencies.clear() should have thrown an Exception");
- } catch (Exception expected) {
- }
-
- // get the dependency set
- dependencies = serviceFactory.getStopConditions();
- assertNotNull(dependencies);
- // it should be initially empty
- assertTrue(dependencies.isEmpty());
-
- serviceFactory.addStopCondition(STOP_CONDITION);
- // old dependency set should still be empty... it is a snapshot
- assertTrue(dependencies.isEmpty());
-
- // get a new dependency set
- dependencies = serviceFactory.getStopConditions();
- assertNotNull(dependencies);
- // should have our dependency in it
- assertEquals(1, dependencies.size());
- assertTrue(dependencies.contains(STOP_CONDITION));
-
- try {
- dependencies.clear();
- fail("dependencies.clear() should have thrown an Exception");
- } catch (Exception expected) {
- }
- }
-
- /**
- * Tests that getTypes returns an array containing a single class which is the class of the service passed to the constuctor.
- */
- public void testGetTypes() {
- StaticServiceFactory serviceFactory = new StaticServiceFactory(SERVICE);
- Class[] types = serviceFactory.getTypes();
- assertNotNull(types);
- assertEquals(1, types.length);
- assertSame(SERVICE.getClass(), types[0]);
- }
-
- private static class MockServiceContext implements ServiceContext {
- public Kernel getKernel() {
- throw new UnsupportedOperationException();
- }
-
- public ServiceName getServiceName() {
- throw new UnsupportedOperationException();
- }
-
- public ClassLoader getClassLoader() {
- throw new UnsupportedOperationException();
- }
- }
-
- private static class MockStartCondition implements ServiceCondition {
- public void initialize(ServiceConditionContext context) {
- throw new UnsupportedOperationException();
- }
-
- public boolean isSatisfied() {
- throw new UnsupportedOperationException();
- }
-
- public void destroy() {
- throw new UnsupportedOperationException();
- }
- }
-
- private static class MockStopCondition implements ServiceCondition {
- public void initialize(ServiceConditionContext context) {
- throw new UnsupportedOperationException();
- }
-
- public boolean isSatisfied() {
- throw new UnsupportedOperationException();
- }
-
- public void destroy() {
- throw new UnsupportedOperationException();
- }
- }
-}
diff --git a/kernel/src/test/org/xbean/kernel/StringServiceNameTest.java b/kernel/src/test/org/xbean/kernel/StringServiceNameTest.java
deleted file mode 100644
index 4e277cc5..00000000
--- a/kernel/src/test/org/xbean/kernel/StringServiceNameTest.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel;
-
-import junit.framework.TestCase;
-
-/**
- * Tests StringServiceName.
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class StringServiceNameTest extends TestCase {
- /**
- * Tests that the constuctor works when passed a string and fails when passed null.
- */
- public void testConstructor() {
- new StringServiceName("foo");
-
- try {
- new StringServiceName(null);
- fail("new StringServiceName(null) should have thrown NullPointerException");
- } catch (NullPointerException expected) {
- }
- }
-
- /**
- * Tests that toString returns equivalent String as the one passed to the construcor.
- */
- public void testToString() {
- assertEquals("foo", new StringServiceName("foo").toString());
- }
-
- /**
- * Tests that equals works when comparing two names created with equivalent strings, and fails when not.
- */
- public void testEquals() {
- assertEquals(new StringServiceName("foo"), new StringServiceName("foo"));
- assertFalse(new StringServiceName("bar").equals(new StringServiceName("foo")));
- }
-
- /**
- * Tests that hashCode creates the same value when used on two names created with equivalent strings, and fails when not.
- */
- public void testHashCode() {
- assertEquals(new StringServiceName("foo").hashCode(), new StringServiceName("foo").hashCode());
- assertFalse(new StringServiceName("bar").hashCode() == new StringServiceName("foo").hashCode());
- }
-}
diff --git a/kernel/src/test/org/xbean/kernel/standard/ServiceManagerRegistryTest.java b/kernel/src/test/org/xbean/kernel/standard/ServiceManagerRegistryTest.java
deleted file mode 100644
index 46d407f3..00000000
--- a/kernel/src/test/org/xbean/kernel/standard/ServiceManagerRegistryTest.java
+++ /dev/null
@@ -1,897 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.Arrays;
-import java.util.AbstractCollection;
-import java.util.AbstractSet;
-import java.util.Collection;
-import java.util.SortedSet;
-import java.util.TreeSet;
-import java.io.Serializable;
-
-import edu.emory.mathcs.backport.java.util.concurrent.Callable;
-import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
-import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
-import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-import junit.framework.TestCase;
-import org.xbean.kernel.IllegalServiceStateException;
-import org.xbean.kernel.KernelErrorsError;
-import org.xbean.kernel.ServiceAlreadyExistsException;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.ServiceNotFoundException;
-import org.xbean.kernel.ServiceRegistrationException;
-import org.xbean.kernel.StaticServiceFactory;
-import org.xbean.kernel.StopStrategies;
-import org.xbean.kernel.StopStrategy;
-import org.xbean.kernel.StringServiceName;
-import org.xbean.kernel.UnsatisfiedConditionsException;
-import org.xbean.kernel.NullServiceMonitor;
-
-/**
- * Test the ServiceManagerRegistry.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceManagerRegistryTest extends TestCase {
- private static final int TIMEOUT_DURATION = 5;
- private static final TimeUnit TIMEOUT_UNITS = TimeUnit.SECONDS;
-
- private static final StringServiceName SERVICE_NAME = new StringServiceName("Service");
- private static final StaticServiceFactory SERVICE_FACTORY = new StaticServiceFactory(new Object());
- private static final ClassLoader CLASS_LOADER = new URLClassLoader(new URL[0]);
- private static final Class[] EXPECTED_TYPES = new Class[] {
- TreeSet.class,
- AbstractSet.class,
- AbstractCollection.class,
- Object.class,
- SortedSet.class,
- Set.class,
- Collection.class,
- Cloneable.class,
- Serializable.class,
- List.class
- };
- private final MockServiceManager serviceManager = new MockServiceManager();
- private final MockServiceManagerFactory serviceManagerFactory = new MockServiceManagerFactory();
- private final ServiceManagerRegistry registry = new ServiceManagerRegistry(serviceManagerFactory);
-
- /**
- * Tests the initial state of the registry.
- */
- public void testInitialState() {
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- assertFalse(serviceManager.isInitializeCalled());
- assertFalse(serviceManager.isDestroyCalled());
- }
-
- /**
- * Test the registration and unregistration.
- * Strategy:
- *
- * Register a service
- *
- * Verify the service was registered and callbacks were made
- *
- * Unregister the service
- *
- * Verify the service was unregistered and callbacks were made
- *
- *
- * @throws Exception if there is a failure
- */
- public void testRegisterUnregister() throws Exception {
- register();
- unregister();
- }
-
- /**
- * Tests the destroy method.
- * Strategy:
- *
- * Register a service
- *
- * Verify the service was registered and callbacks were made
- *
- * Destroy the registry
- *
- * Verify the service was stopped and callbacks were made
- *
- *
- * @throws Exception if there is a failure
- */
- public void testDestroy() throws Exception {
- register();
- destroy();
- }
-
- /**
- * Tests that an exception is thrown if an attempt is made to register.
- *
- * @throws Exception if there is a failure
- */
- public void testDoubleRegister() throws Exception {
- register();
- try {
- registry.registerService(SERVICE_NAME, SERVICE_FACTORY, CLASS_LOADER);
- fail("should have thrown an exception");
- } catch (ServiceAlreadyExistsException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- }
-
- /**
- * Test that an attempt to unregister a service that is not registered throws an exception.
- *
- * @throws Exception if there is a failure
- */
- public void testUnregisterUnknown() throws Exception {
- try {
- registry.unregisterService(SERVICE_NAME, StopStrategies.SYNCHRONOUS);
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- }
-
- /**
- * Tests that when the initialize method throws an exception the service is not registered.
- *
- * @throws Exception if there is a failure
- */
- public void testRegisterException() throws Exception {
- register(new Exception("register exception"));
- register(new RuntimeException("register runtime exception"));
- register(new Error("register error"));
- register();
- unregister();
- }
-
- /**
- * Tests that when the destroy method throws an exception the service is not unregistered.
- *
- * @throws Exception if there is a failure
- */
- public void testUnregisterException() throws Exception {
- register();
- unregister(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET));
- unregister(new IllegalServiceStateException("destroy exception", SERVICE_NAME));
- unregister(new RuntimeException("destroy exception"));
- unregister(new Error("destroy exception"));
- unregister();
- }
-
- /**
- * Tests that when the destroy and/or stop methods throw an exception during registry destroy, that destruction
- * continues and the exceptions are thrown in a single KernelErrorsError.
- *
- * @throws Exception if there is a failure
- */
- public void testDestroyException() throws Exception {
- register();
- destroy(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET), null);
- register();
- destroy(new RuntimeException("destroy exception"), null);
- register();
- destroy(new Error("destroy exception"), null);
- register();
- destroy(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET), new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET));
- register();
- destroy(new RuntimeException("destroy exception"), new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET));
- register();
- destroy(new Error("destroy exception"), new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET));
- register();
- destroy(null, new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET));
- register();
- destroy(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET), new IllegalServiceStateException("destroy exception", SERVICE_NAME));
- register();
- destroy(new RuntimeException("destroy exception"), new IllegalServiceStateException("destroy exception", SERVICE_NAME));
- register();
- destroy(new Error("destroy exception"), new IllegalServiceStateException("destroy exception", SERVICE_NAME));
- register();
- destroy(null, new IllegalServiceStateException("destroy exception", SERVICE_NAME));
- register();
- destroy(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET), new RuntimeException("destroy exception"));
- register();
- destroy(new RuntimeException("destroy exception"), new RuntimeException("destroy exception"));
- register();
- destroy(new Error("destroy exception"), new RuntimeException("destroy exception"));
- register();
- destroy(null, new RuntimeException("destroy exception"));
- register();
- destroy(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET), new Error("destroy exception"));
- register();
- destroy(new RuntimeException("destroy exception"), new Error("destroy exception"));
- register();
- destroy(new Error("destroy exception"), new Error("destroy exception"));
- register();
- destroy(null, new Error("destroy exception"));
- }
-
- /**
- * Tests that when a service manager blocks during registration, that the registry blocks isRegistered and
- * getServiceManager calls until the registration completes.
- *
- * @throws Exception if there is a failure
- */
- public void testRegisterWaiting() throws Exception {
- registerWaiting(null);
- }
-
- /**
- * Tests that when a service manager blocks during registration and then throws an exception, that the registry
- * blocks isRegistered and getServiceManager calls until the registration completes, and then returns the correct
- * values for an unregistered service.
- *
- * @throws Exception if there is a failure
- */
- public void testRegisterWaitingException() throws Exception {
- registerWaiting(new Exception("register exception"));
- registerWaiting(new RuntimeException("register runtime exception"));
- registerWaiting(new Error("register error"));
- }
-
- /**
- * Tests that when a blocking service throws an exception during registration a nother service can wait be waiting
- * to take over the registration from the failed thread.
- *
- * @throws Exception if there is a failure
- */
- public void testDoubleRegisterWaiting() throws Exception {
- // start thread to attempt to register but throw an exception
- FutureTask registerFailTask = registerWaitingTask(new Exception("register exception"));
-
- // start thread to successfully register
- final CountDownLatch registerStartedSignal = new CountDownLatch(1);
- final MockServiceManager newServiceManager = new MockServiceManager();
- newServiceManager.setWait();
- FutureTask registerTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- registerStartedSignal.countDown();
- register(null, newServiceManager);
- return Boolean.TRUE;
- }
- });
- Thread registerThread = new Thread(registerTask, "registerTask");
- registerThread.setDaemon(true);
- registerThread.start();
- registerStartedSignal.await(TIMEOUT_DURATION, TIMEOUT_UNITS);
-
- // sleep a bit to assure that the register thread entered the registry code
- Thread.sleep(100);
-
- // verify all are not done
- assertFalse(registerFailTask.isDone());
- assertFalse(registerTask.isDone());
-
- // finish register fail, and verify it failed
- serviceManager.signalExit();
- assertEquals(Boolean.FALSE, registerFailTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
-
- // verify success registration and verify itworked
- newServiceManager.signalExit();
- assertEquals(Boolean.TRUE, registerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- assertTrue(registry.isRegistered(SERVICE_NAME));
- assertEquals(newServiceManager, registry.getServiceManager(SERVICE_NAME));
- }
-
- /**
- * Tests that when a service manager blocks during unregistration, that the registry blocks isRegistered and
- * getServiceManager calls until the unregistration completes.
- *
- * @throws Exception if there is a failure
- */
- public void testUnregisterWaiting() throws Exception {
- register();
- unregisterWaiting(null);
- }
-
- /**
- * Tests that when a service manager blocks during unregistration and then throws an exception, that the registry
- * blocks isRegistered and getServiceManager calls until the unregistration completes, and then returns the correct
- * values for an unregistered service.
- *
- * @throws Exception if there is a failure
- */
- public void testUnregisterWaitingException() throws Exception {
- register();
- unregisterWaiting(new UnsatisfiedConditionsException("destroy exception", SERVICE_NAME, Collections.EMPTY_SET));
- unregisterWaiting(new IllegalServiceStateException("destroy exception", SERVICE_NAME));
- unregisterWaiting(new RuntimeException("destroy exception"));
- unregisterWaiting(new Error("destroy exception"));
- unregisterWaiting(null);
- }
-
- /**
- * Tests that when a service manager blocks during destroy, that the registry does not block isRegistered and
- * getServiceManager calls, and then returns the correct values for an unregistered service.
- *
- * @throws Exception if there is a failure
- */
- public void testDestroyWaiting() throws Exception {
- register();
-
- // start thread to destroy and wait
- FutureTask destroyTask = destroyWaitingTask();
-
- // verify all are not done
- assertFalse(destroyTask.isDone());
-
- // verify that the service already appears to be unregistered
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
-
- // finish register
- serviceManager.signalExit();
-
- // verify registration worked
- assertEquals(Boolean.TRUE, destroyTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- }
-
- private void registerWaiting(Throwable throwable) throws Exception {
- // start thread to register and wait
- FutureTask registerTask = registerWaitingTask(throwable);
-
- // start thread to attempt getService
- FutureTask getServiceManagerTask = getServiceWaiting();
-
- // start thread to attempt isRegistered
- FutureTask isRegisteredTask = isRegisteredWaiting();
-
- // not necessary, but sleep a bit anyway
- Thread.sleep(100);
-
- // verify all are not done
- assertFalse(registerTask.isDone());
- assertFalse(getServiceManagerTask.isDone());
- assertFalse(isRegisteredTask.isDone());
-
- // finish register
- serviceManager.signalExit();
-
- if (throwable == null) {
- // verify registration worked
- assertEquals(Boolean.TRUE, registerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- assertTrue(registry.isRegistered(SERVICE_NAME));
- assertEquals(serviceManager, registry.getServiceManager(SERVICE_NAME));
-
- // verify waiting isRegistered worked
- assertEquals(Boolean.TRUE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
-
- // verify getServiceManager worked
- assertEquals(serviceManager, getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- } else {
- // verify registration failed
- assertEquals(Boolean.FALSE, registerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
-
- // verify waiting isRegistered worked
- assertEquals(Boolean.FALSE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
-
- // verify getServiceManager worked
- try {
- getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS);
- fail("should have thrown an exception");
- } catch (ExecutionException e) {
- assertTrue(e.getCause() instanceof ServiceNotFoundException);
- ServiceNotFoundException serviceNotFoundException = (ServiceNotFoundException) e.getCause();
- assertSame(SERVICE_NAME, serviceNotFoundException.getServiceName());
- }
- }
- }
-
- private FutureTask registerWaitingTask(final Throwable throwable) throws InterruptedException {
- serviceManager.setWait();
- FutureTask registerTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- register(throwable);
- return Boolean.valueOf(throwable == null);
- }
- });
- Thread registerThread = new Thread(registerTask, throwable == null ? "registerTask" : "registerExceptionTask");
- registerThread.setDaemon(true);
- registerThread.start();
-
- // wait for register to block
- assertTrue(serviceManager.awaitEnterSignal());
- return registerTask;
- }
-
- private void unregisterWaiting(Throwable throwable) throws Exception {
- // start thread to unregister and wait
- FutureTask unregisterTask = unregisterWaitingTask(throwable);
-
- // start thread to attempt getService
- FutureTask getServiceManagerTask = getServiceWaiting();
-
- // start thread to attempt isRegistered
- FutureTask isRegisteredTask = isRegisteredWaiting();
-
- // not necessary, but sleep a bit anyway
- Thread.sleep(100);
-
- // verify all are not done
- assertFalse(unregisterTask.isDone());
- assertFalse(getServiceManagerTask.isDone());
- assertFalse(isRegisteredTask.isDone());
-
- // finish register
- serviceManager.signalExit();
-
- if (throwable == null) {
- // verify registration worked
- assertEquals(Boolean.TRUE, unregisterTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
-
- // verify waiting isRegistered worked
- assertEquals(Boolean.FALSE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
-
- // verify getServiceManager worked
- try {
- getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS);
- fail("should have thrown an exception");
- } catch (ExecutionException e) {
- assertTrue(e.getCause() instanceof ServiceNotFoundException);
- ServiceNotFoundException serviceNotFoundException = (ServiceNotFoundException) e.getCause();
- assertSame(SERVICE_NAME, serviceNotFoundException.getServiceName());
- }
- } else {
- // verify unregistration failed
- assertEquals(Boolean.FALSE, unregisterTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- assertTrue(registry.isRegistered(SERVICE_NAME));
- assertEquals(serviceManager, registry.getServiceManager(SERVICE_NAME));
-
- // verify waiting isRegistered worked
- assertEquals(Boolean.TRUE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
-
- // verify getServiceManager worked
- assertEquals(serviceManager, getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS));
- }
- }
-
- private FutureTask unregisterWaitingTask(final Throwable throwable) throws InterruptedException {
- serviceManager.setWait();
- FutureTask unregisterTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- unregister(throwable);
- return Boolean.valueOf(throwable == null);
- }
- });
- Thread unregisterThread = new Thread(unregisterTask, throwable == null ? "unregisterTask" : "unregisterExceptionTask");
- unregisterThread.setDaemon(true);
- unregisterThread.start();
-
- // wait for register to block
- assertTrue(serviceManager.awaitEnterSignal());
- return unregisterTask;
- }
-
- private FutureTask destroyWaitingTask() throws InterruptedException {
- serviceManager.setWait();
- FutureTask unregisterTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- destroy();
- return Boolean.TRUE;
- }
- });
- Thread destroyThread = new Thread(unregisterTask, "destroyTask");
- destroyThread.setDaemon(true);
- destroyThread.start();
-
- // wait for register to block
- assertTrue(serviceManager.awaitEnterSignal());
- return unregisterTask;
- }
-
- private FutureTask getServiceWaiting() throws InterruptedException {
- final CountDownLatch getServiceManagerSignal = new CountDownLatch(1);
- FutureTask getServiceManagerTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- getServiceManagerSignal.countDown();
- return registry.getServiceManager(SERVICE_NAME);
- }
- });
- Thread getServiceManagerThread = new Thread(getServiceManagerTask, "getServiceManagerTask");
- getServiceManagerThread.setDaemon(true);
- getServiceManagerThread.start();
-
- // wait for thread started
- getServiceManagerSignal.await(TIMEOUT_DURATION, TIMEOUT_UNITS);
- return getServiceManagerTask;
- }
-
- private FutureTask isRegisteredWaiting() throws InterruptedException {
- final CountDownLatch isRegisteredSignal = new CountDownLatch(1);
- FutureTask isRegisteredTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- isRegisteredSignal.countDown();
- return Boolean.valueOf(registry.isRegistered(SERVICE_NAME));
- }
- });
- Thread isRegisteredThread = new Thread(isRegisteredTask, "isRegisteredTask");
- isRegisteredThread.setDaemon(true);
- isRegisteredThread.start();
-
- // wait for thread started
- isRegisteredSignal.await(TIMEOUT_DURATION, TIMEOUT_UNITS);
- return isRegisteredTask;
- }
-
- private void register() throws Exception {
- register(null, serviceManager);
- }
-
- private void register(Throwable throwable) throws Exception {
- register(throwable, serviceManager);
- }
-
- private void register(Throwable throwable, MockServiceManager serviceManager) throws Exception {
- serviceManager.reset();
- serviceManager.setInitializeException(throwable);
- serviceManagerFactory.addServiceManager(serviceManager);
- try {
- registry.registerService(SERVICE_NAME, SERVICE_FACTORY, CLASS_LOADER);
- assertNull(throwable);
- } catch (ServiceRegistrationException expected) {
- // expected
- assertSame(throwable, expected.getCause());
- assertSame(SERVICE_NAME, expected.getServiceName());
- }
-
- if (throwable == null) {
- assertTrue(registry.isRegistered(SERVICE_NAME));
- assertSame(serviceManager, registry.getServiceManager(SERVICE_NAME));
- for (int i = 0; i < EXPECTED_TYPES.length; i++) {
- assertSame(serviceManager, registry.getServiceManager(EXPECTED_TYPES[i]));
- assertTrue(registry.getServiceManagers(EXPECTED_TYPES[i]).contains(serviceManager));
- }
- } else {
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- for (int i = 0; i < EXPECTED_TYPES.length; i++) {
- assertNull(registry.getServiceManager(EXPECTED_TYPES[i]));
- assertTrue(registry.getServiceManagers(EXPECTED_TYPES[i]).isEmpty());
- }
- }
- assertTrue(serviceManager.isInitializeCalled());
- assertFalse(serviceManager.isDestroyCalled());
- }
-
- private void unregister() throws ServiceNotFoundException {
- unregister(null);
- }
-
- private void unregister(Throwable throwable) throws ServiceNotFoundException {
- serviceManager.reset();
- serviceManager.setDestroyException(throwable);
- try {
- registry.unregisterService(SERVICE_NAME, StopStrategies.SYNCHRONOUS);
- assertNull(throwable);
- } catch (ServiceRegistrationException expected) {
- // expected
- assertSame(SERVICE_NAME, expected.getServiceName());
- assertSame(throwable, expected.getCause());
- }
-
- if (throwable == null) {
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- for (int i = 0; i < EXPECTED_TYPES.length; i++) {
- assertNull(registry.getServiceManager(EXPECTED_TYPES[i]));
- assertTrue(registry.getServiceManagers(EXPECTED_TYPES[i]).isEmpty());
- }
- } else {
- assertTrue(registry.isRegistered(SERVICE_NAME));
- assertSame(serviceManager, registry.getServiceManager(SERVICE_NAME));
- for (int i = 0; i < EXPECTED_TYPES.length; i++) {
- assertSame(serviceManager, registry.getServiceManager(EXPECTED_TYPES[i]));
- assertTrue(registry.getServiceManagers(EXPECTED_TYPES[i]).contains(serviceManager));
- }
- }
- assertFalse(serviceManager.isInitializeCalled());
- assertTrue(serviceManager.isDestroyCalled());
- }
-
- private void destroy() {
- destroy(null, null);
- }
-
- private void destroy(Throwable stopException, Throwable destroyException) {
- serviceManager.reset();
- serviceManager.setStopException(stopException);
- serviceManager.setDestroyException(destroyException);
-
- try {
- registry.destroy();
- assertNull(stopException);
- assertNull(destroyException);
- } catch (KernelErrorsError kernelErrorsError) {
- List errors = new ArrayList(kernelErrorsError.getErrors());
- if (stopException != null) {
- assertTrue(errors.size() >= 3);
- assertTrue(errors.get(0) instanceof AssertionError);
- assertSame(stopException, ((AssertionError) errors.get(0)).getCause());
- assertTrue(errors.get(1) instanceof AssertionError);
- assertSame(stopException, ((AssertionError) errors.get(1)).getCause());
- assertTrue(errors.get(2) instanceof AssertionError);
- assertSame(stopException, ((AssertionError) errors.get(2)).getCause());
- errors = errors.subList(3, errors.size());
- }
-
- if (destroyException != null) {
- assertEquals(1, errors.size());
- assertTrue(errors.get(0) instanceof AssertionError);
- assertSame(destroyException, ((AssertionError) errors.get(0)).getCause());
- errors = Collections.EMPTY_LIST;
- }
-
- assertEquals(Collections.EMPTY_LIST, errors);
- }
-
- assertFalse(registry.isRegistered(SERVICE_NAME));
- try {
- assertNull(registry.getServiceManager(SERVICE_NAME));
- fail("should have thrown an exception");
- } catch (ServiceNotFoundException expected) {
- // expected
- assertEquals(SERVICE_NAME, expected.getServiceName());
- }
- assertFalse(serviceManager.isInitializeCalled());
- assertTrue(serviceManager.isStopCalled());
- assertTrue(serviceManager.isDestroyCalled());
- }
-
- private static class MockServiceManagerFactory extends ServiceManagerFactory {
- private final LinkedList serviceManagers = new LinkedList();
-
- private MockServiceManagerFactory() {
- super(null, null, null, 0, null);
- }
-
- public ServiceManager createServiceManager(long serviceId, ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader) {
- assertEquals(SERVICE_NAME, serviceName);
- assertEquals(SERVICE_FACTORY, serviceFactory);
- assertEquals(CLASS_LOADER, classLoader);
- synchronized (serviceManagers) {
- return (ServiceManager) serviceManagers.removeFirst();
- }
- }
-
- public void addServiceManager(ServiceManager serviceManager) {
- synchronized (serviceManagers) {
- serviceManagers.add(serviceManager);
- }
- }
- }
-
- private static class MockServiceManager extends ServiceManager {
- private boolean initializeCalled;
- private boolean destroyCalled;
- private boolean stopCalled;
- private Throwable initializeException;
- private Throwable destroyException;
- private Throwable stopException;
- private CountDownLatch enterWaiting = new CountDownLatch(1);
- private CountDownLatch exitWaiting = new CountDownLatch(0);
- private static final Set TYPES = Collections.unmodifiableSet(new HashSet(Arrays.asList(
- new Class[] {TreeSet.class, List.class} )));
-
-
- private MockServiceManager() {
- super(null,
- 0,
- new StringServiceName("MockService"),
- new StaticServiceFactory(new Object()),
- null,
- new NullServiceMonitor(),
- 0,
- null);
- }
-
- private synchronized void reset() {
- initializeCalled = false;
- destroyCalled = false;
- stopCalled = false;
- initializeException = null;
- destroyException = null;
- stopException = null;
- }
-
- public Set getServiceTypes() {
- return TYPES;
- }
-
- public void initialize() throws IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- synchronized (this) {
- assertFalse(initializeCalled);
- initializeCalled = true;
- }
-
- signalEnter();
- awaitExitSignal();
-
- synchronized (this) {
- if (initializeException instanceof Exception) {
- throw (Exception) initializeException;
- } else if (initializeException instanceof Error) {
- throw (Error) initializeException;
- }
- }
- }
-
- public void destroy(StopStrategy stopStrategy) throws IllegalServiceStateException, UnsatisfiedConditionsException {
- synchronized (this) {
- assertFalse(destroyCalled);
- destroyCalled = true;
- }
-
- try {
- signalEnter();
- awaitExitSignal();
- } catch (InterruptedException e) {
- fail("destroyCondition.await() threw an exception");
- }
-
- synchronized (this) {
- if (destroyException instanceof UnsatisfiedConditionsException) {
- throw (UnsatisfiedConditionsException) destroyException;
- } else if (destroyException instanceof IllegalServiceStateException) {
- throw (IllegalServiceStateException) destroyException;
- } else if (destroyException instanceof RuntimeException) {
- throw (RuntimeException) destroyException;
- } else if (destroyException instanceof Error) {
- throw (Error) destroyException;
- }
- }
- }
-
- public synchronized boolean stop(StopStrategy stopStrategy) throws UnsatisfiedConditionsException {
- stopCalled = true;
-
- if (stopException instanceof UnsatisfiedConditionsException) {
- throw (UnsatisfiedConditionsException) stopException;
- } else if (stopException instanceof RuntimeException) {
- throw (RuntimeException) stopException;
- } else if (stopException instanceof Error) {
- throw (Error) stopException;
- }
- return true;
- }
-
- public synchronized boolean isInitializeCalled() {
- return initializeCalled;
- }
-
- public synchronized boolean isDestroyCalled() {
- return destroyCalled;
- }
-
- public synchronized boolean isStopCalled() {
- return stopCalled;
- }
-
- public synchronized void setInitializeException(Throwable initializeException) {
- this.initializeException = initializeException;
- }
-
- public synchronized void setDestroyException(Throwable destroyException) {
- this.destroyException = destroyException;
- }
-
- public synchronized void setStopException(Throwable stopException) {
- this.stopException = stopException;
- }
-
- public boolean awaitEnterSignal() throws InterruptedException {
- CountDownLatch signal;
- synchronized (this) {
- signal = enterWaiting;
- }
- boolean worked = signal.await(TIMEOUT_DURATION, TIMEOUT_UNITS);
- return worked;
- }
-
- private void signalEnter() {
- CountDownLatch signal;
- synchronized (this) {
- signal = enterWaiting;
- }
- signal.countDown();
- }
-
- public synchronized void setWait() {
- exitWaiting = new CountDownLatch(1);
- enterWaiting = new CountDownLatch(1);
- }
-
- public void signalExit() {
- CountDownLatch signal;
- synchronized (this) {
- signal = exitWaiting;
- }
- signal.countDown();
- }
-
- private void awaitExitSignal() throws InterruptedException {
- CountDownLatch signal;
- synchronized (this) {
- signal = exitWaiting;
- }
- signal.await(TIMEOUT_DURATION, TIMEOUT_UNITS);
- }
- }
-}
diff --git a/kernel/src/test/org/xbean/kernel/standard/ServiceManagerTest.java b/kernel/src/test/org/xbean/kernel/standard/ServiceManagerTest.java
deleted file mode 100644
index df2319f7..00000000
--- a/kernel/src/test/org/xbean/kernel/standard/ServiceManagerTest.java
+++ /dev/null
@@ -1,1723 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-
-import edu.emory.mathcs.backport.java.util.concurrent.Callable;
-import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
-import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-import junit.framework.TestCase;
-import org.xbean.kernel.ForcedStopException;
-import org.xbean.kernel.IllegalServiceStateException;
-import org.xbean.kernel.Kernel;
-import org.xbean.kernel.KernelMonitor;
-import org.xbean.kernel.ServiceAlreadyExistsException;
-import org.xbean.kernel.ServiceCondition;
-import org.xbean.kernel.ServiceConditionContext;
-import org.xbean.kernel.ServiceContext;
-import org.xbean.kernel.ServiceEvent;
-import org.xbean.kernel.ServiceFactory;
-import org.xbean.kernel.ServiceMonitor;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.ServiceNotFoundException;
-import org.xbean.kernel.ServiceRegistrationException;
-import org.xbean.kernel.ServiceState;
-import org.xbean.kernel.StartStrategies;
-import org.xbean.kernel.StartStrategy;
-import org.xbean.kernel.StaticServiceFactory;
-import org.xbean.kernel.StopStrategies;
-import org.xbean.kernel.StopStrategy;
-import org.xbean.kernel.StringServiceName;
-import org.xbean.kernel.UnregisterServiceException;
-import org.xbean.kernel.UnsatisfiedConditionsException;
-
-/**
- * Test ServiceManager.
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceManagerTest extends TestCase {
- private static final Object SERVICE = new Object();
- private final MockKernel kernel = new MockKernel();
- private final StringServiceName serviceName = new StringServiceName("Service");
- private final StringServiceName ownedServiceName = new StringServiceName("OwnedService");
- private final MockStartCondition startCondition = new MockStartCondition();
- private final MockStopCondition stopCondition = new MockStopCondition();
- private final MockServiceFactory serviceFactory = new MockServiceFactory();
- private final ClassLoader classLoader = new URLClassLoader(new URL[0]);
- private final MockServiceMonitor serviceMonitor = new MockServiceMonitor();
- private ServiceManager serviceManager;
-
- /**
- * Tests that the initial state of the service manager is as expected.
- */
- public void testInitialState() {
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- }
-
- /**
- * Tests that initialize and destroy work without exception.
- * @throws Exception if a problem occurs
- */
- public void testInitializeDestroy() throws Exception {
- initialize();
- destroy(StopStrategies.SYNCHRONOUS);
- initialize();
- destroy(StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * Tests that start and stop work without exception.
- * @throws Exception if a problem occurs
- */
- public void testStartStop() throws Exception {
- startStop(StartStrategies.ASYNCHRONOUS, false);
- startStop(StartStrategies.SYNCHRONOUS, false);
- startStop(StartStrategies.UNREGISTER, false);
- startStop(StartStrategies.BLOCK, false);
-
- startStop(StartStrategies.ASYNCHRONOUS, false);
- startStop(StartStrategies.SYNCHRONOUS, false);
- startStop(StartStrategies.UNREGISTER, false);
- startStop(StartStrategies.BLOCK, false);
- }
-
- /**
- * Tests that startRecursive results in recursive start calls on the owned services.
- * @throws Exception if a problem occurs
- */
- public void testStartRecursive() throws Exception {
- startStop(StartStrategies.ASYNCHRONOUS, true);
- startStop(StartStrategies.SYNCHRONOUS, true);
- startStop(StartStrategies.UNREGISTER, true);
- startStop(StartStrategies.BLOCK, true);
-
- startStop(StartStrategies.ASYNCHRONOUS, true);
- startStop(StartStrategies.SYNCHRONOUS, true);
- startStop(StartStrategies.UNREGISTER, true);
- startStop(StartStrategies.BLOCK, true);
- }
-
- private void startStop(StartStrategy startStrategy, boolean recursive) throws Exception {
- start(recursive, startStrategy);
- stop(StopStrategies.SYNCHRONOUS);
-
- start(recursive, startStrategy);
- start(recursive, startStrategy);
- stop(StopStrategies.SYNCHRONOUS);
- stop(StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * Tests how the start strategies respond when an Exception is thrown.
- * @throws Exception if a problem occurs
- */
- public void testStartException() throws Exception {
- startException(StartStrategies.ASYNCHRONOUS, false);
- startException(StartStrategies.SYNCHRONOUS, false);
- startException(StartStrategies.UNREGISTER, false);
- startException(StartStrategies.BLOCK, false);
-
- startException(StartStrategies.ASYNCHRONOUS, false);
- startException(StartStrategies.SYNCHRONOUS, false);
- startException(StartStrategies.UNREGISTER, false);
- startException(StartStrategies.BLOCK, false);
- }
-
- /**
- * Tests how startRecursive start strategies respond when an Exception is thrown.
- * @throws Exception if a problem occurs
- */
- public void testStartExceptionRecursive() throws Exception {
- startException(StartStrategies.ASYNCHRONOUS, true);
- startException(StartStrategies.SYNCHRONOUS, true);
- startException(StartStrategies.UNREGISTER, true);
- startException(StartStrategies.BLOCK, true);
-
- startException(StartStrategies.ASYNCHRONOUS, true);
- startException(StartStrategies.SYNCHRONOUS, true);
- startException(StartStrategies.UNREGISTER, true);
- startException(StartStrategies.BLOCK, true);
- }
-
- private void startException(StartStrategy startStrategy, boolean recursive) throws Exception {
- serviceFactory.throwExceptionFromCreate = true;
- try {
- start(recursive, startStrategy);
- } catch (MockCreateException e) {
- assertTrue(startStrategy == StartStrategies.SYNCHRONOUS || startStrategy == StartStrategies.BLOCK);
- assertEquals(serviceFactory.createException, e);
- } catch (UnregisterServiceException e) {
- assertEquals(StartStrategies.UNREGISTER, startStrategy);
- assertSame(serviceFactory.createException, e.getCause());
- assertEquals(serviceName, e.getServiceName());
- }
- stop(StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * Tests how the start strategies respond when confronted with an unsatisfied start condition.
- * @throws Exception if a problem occurs
- */
- public void testStartWaiting() throws Exception {
- startWaiting(StartStrategies.ASYNCHRONOUS, false);
- startWaiting(StartStrategies.SYNCHRONOUS, false);
- startWaiting(StartStrategies.UNREGISTER, false);
-
- startWaiting(StartStrategies.ASYNCHRONOUS, false);
- startWaiting(StartStrategies.SYNCHRONOUS, false);
- startWaiting(StartStrategies.UNREGISTER, false);
- }
-
- /**
- * Tests how the startRecursive start strategies responed when confronted with an unsatisfied start condition.
- * @throws Exception
- */
- public void testStartWaitingRecursive() throws Exception {
- startWaiting(StartStrategies.ASYNCHRONOUS, true);
- startWaiting(StartStrategies.SYNCHRONOUS, true);
- startWaiting(StartStrategies.UNREGISTER, true);
-
- startWaiting(StartStrategies.ASYNCHRONOUS, true);
- startWaiting(StartStrategies.SYNCHRONOUS, true);
- startWaiting(StartStrategies.UNREGISTER, true);
- }
-
- private void startWaiting(StartStrategy startStrategy, boolean recursive) throws Exception {
- startCondition.satisfied = false;
- try {
- start(recursive, startStrategy);
- } catch (UnsatisfiedConditionsException e) {
- assertTrue(startStrategy == StartStrategies.SYNCHRONOUS);
- assertTrue(e.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, e.getServiceName());
- } catch (UnregisterServiceException e) {
- assertEquals(StartStrategies.UNREGISTER, startStrategy);
- assertEquals(serviceName, e.getServiceName());
- UnsatisfiedConditionsException cause = (UnsatisfiedConditionsException) e.getCause();
- assertTrue(cause.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, cause.getServiceName());
- }
- stop(StopStrategies.SYNCHRONOUS);
- }
-
-
- /**
- * Tests how the start strategies respond once a once unsatisfied start condition becomes satisfied.
- * @throws Exception if a problem occurs
- */
- public void testStartWaitingStart() throws Exception {
- startWaitingStart(StartStrategies.ASYNCHRONOUS, false);
- startWaitingStart(StartStrategies.SYNCHRONOUS, false);
- startWaitingStart(StartStrategies.UNREGISTER, false);
-
- startWaitingStart(StartStrategies.ASYNCHRONOUS, false);
- startWaitingStart(StartStrategies.SYNCHRONOUS, false);
- startWaitingStart(StartStrategies.UNREGISTER, false);
- }
-
- /**
- * Tests how startRecursive start strategies respond once a once unsatisfied start condition becomes satisfied.
- * @throws Exception if a problem occurs
- */
- public void testStartWaitingStartRecursive() throws Exception {
- startWaitingStart(StartStrategies.ASYNCHRONOUS, true);
- startWaitingStart(StartStrategies.SYNCHRONOUS, true);
- startWaitingStart(StartStrategies.UNREGISTER, true);
-
- startWaitingStart(StartStrategies.ASYNCHRONOUS, true);
- startWaitingStart(StartStrategies.SYNCHRONOUS, true);
- startWaitingStart(StartStrategies.UNREGISTER, true);
- }
-
- private void startWaitingStart(StartStrategy startStrategy, boolean recursive) throws Exception {
- startCondition.satisfied = recursive;
- try {
- start(recursive, startStrategy);
- } catch (UnsatisfiedConditionsException e) {
- assertTrue(startStrategy == StartStrategies.SYNCHRONOUS);
- assertTrue(e.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, e.getServiceName());
- } catch (UnregisterServiceException e) {
- assertEquals(StartStrategies.UNREGISTER, startStrategy);
- assertEquals(serviceName, e.getServiceName());
- UnsatisfiedConditionsException cause = (UnsatisfiedConditionsException) e.getCause();
- assertTrue(cause.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, cause.getServiceName());
- }
- try {
- start(recursive, startStrategy);
- } catch (UnsatisfiedConditionsException e) {
- assertTrue(startStrategy == StartStrategies.SYNCHRONOUS);
- assertTrue(e.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, e.getServiceName());
- } catch (UnregisterServiceException e) {
- assertEquals(StartStrategies.UNREGISTER, startStrategy);
- assertEquals(serviceName, e.getServiceName());
- UnsatisfiedConditionsException cause = (UnsatisfiedConditionsException) e.getCause();
- assertTrue(cause.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, cause.getServiceName());
- }
- startCondition.satisfied = true;
- start(recursive, startStrategy);
- start(recursive, startStrategy);
- stop(StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * Tests the BLOCK start stragegy.
- * @throws Exception if a problem occurs
- */
- public void testBlockStartWaiting() throws Exception {
- startCondition.satisfied = false;
- startCondition.isSatisfiedSignal = new CountDownLatch(1);
- FutureTask startTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- start(false, StartStrategies.BLOCK);
- return Boolean.TRUE;
- }
- });
- Thread startThread = new Thread(startTask, "StartTask");
- startThread.setDaemon(true);
- startThread.start();
-
- // wait for the start thread to reach the startContion initialize method
- assertTrue(startCondition.isSatisfiedSignal.await(5, TimeUnit.SECONDS));
-
- // we should not have a service instance and be in the starting state
- assertSame(ServiceState.STARTING, serviceManager.getState());
- assertNull(serviceManager.getService());
- assertEquals(0, serviceManager.getStartTime());
- assertNotNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertTrue(startCondition.initializeCalled);
- assertTrue(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- long now = System.currentTimeMillis();
- // introduce a bit of delay so subsequent times are much less likely to equal to not
- Thread.sleep(500);
-
- startCondition.satisfied = true;
- startCondition.context.setSatisfied();
-
- // wait for the start task to complete
- assertEquals(Boolean.TRUE, startTask.get(5, TimeUnit.SECONDS));
-
- // we should be running
- assertSame(ServiceState.RUNNING, serviceManager.getState());
- assertSame(SERVICE, serviceManager.getService());
- assertTrue(serviceManager.getStartTime() >= now);
- assertNotNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNotNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertTrue(startCondition.initializeCalled);
- assertTrue(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- stop(StopStrategies.SYNCHRONOUS);
- }
-
- /**
- * Tests how start responds when the service factory is not enabled.
- * @throws Exception if a problem occurs
- */
- public void testDisabledStart() throws Exception {
- disabledStart(StartStrategies.ASYNCHRONOUS);
- disabledStart(StartStrategies.SYNCHRONOUS);
- disabledStart(StartStrategies.UNREGISTER);
- disabledStart(StartStrategies.BLOCK);
-
- disabledStart(StartStrategies.ASYNCHRONOUS);
- disabledStart(StartStrategies.SYNCHRONOUS);
- disabledStart(StartStrategies.UNREGISTER);
- disabledStart(StartStrategies.BLOCK);
- }
-
- private void disabledStart(StartStrategy startStrategy) throws Exception {
- serviceFactory.setEnabled(false);
- try {
- serviceManager.start(false, startStrategy);
- fail("A disabled service should throw an IllegalServiceStateException from start");
- } catch (IllegalServiceStateException e) {
- // expected
- assertEquals(serviceName, e.getServiceName());
- }
-
- // move to starting disable, move to running, and try to restart
- serviceFactory.setEnabled(true);
- startCondition.satisfied = false;
- start(false, StartStrategies.ASYNCHRONOUS);
-
- serviceFactory.setEnabled(false);
- startCondition.satisfied = true;
- start(false, startStrategy);
- start(false, startStrategy);
- stop(StopStrategies.ASYNCHRONOUS);
- try {
- serviceManager.start(false, startStrategy);
- fail("A disabled service should throw an IllegalServiceStateException from start");
- } catch (IllegalServiceStateException e) {
- // expected
- assertEquals(serviceName, e.getServiceName());
- }
- }
-
- /**
- * Tests how the stop strategies respond when destroyService throws an Exception.
- * @throws Exception if a problem occurs
- */
- public void testStopException() throws Exception {
- stopException(StopStrategies.ASYNCHRONOUS);
- stopException(StopStrategies.SYNCHRONOUS);
- stopException(StopStrategies.FORCE);
- stopException(StopStrategies.BLOCK);
- }
-
- private void stopException(StopStrategy stopStrategy) throws Exception {
- serviceFactory.throwExceptionFromDestroy = true;
- start(false, StartStrategies.SYNCHRONOUS);
- stop(stopStrategy);
- }
-
- /**
- * Tests how the start and stop strategies work when both create and destroy throw exceptions.
- * @throws Exception if a problem occurs
- */
- public void testStartStopsException() throws Exception {
- startStopsException(StartStrategies.ASYNCHRONOUS, StopStrategies.ASYNCHRONOUS);
- startStopsException(StartStrategies.ASYNCHRONOUS, StopStrategies.SYNCHRONOUS);
- startStopsException(StartStrategies.ASYNCHRONOUS, StopStrategies.FORCE);
- startStopsException(StartStrategies.ASYNCHRONOUS, StopStrategies.BLOCK);
- startStopsException(StartStrategies.SYNCHRONOUS, StopStrategies.ASYNCHRONOUS);
- startStopsException(StartStrategies.SYNCHRONOUS, StopStrategies.SYNCHRONOUS);
- startStopsException(StartStrategies.SYNCHRONOUS, StopStrategies.FORCE);
- startStopsException(StartStrategies.SYNCHRONOUS, StopStrategies.BLOCK);
- startStopsException(StartStrategies.UNREGISTER, StopStrategies.ASYNCHRONOUS);
- startStopsException(StartStrategies.UNREGISTER, StopStrategies.SYNCHRONOUS);
- startStopsException(StartStrategies.UNREGISTER, StopStrategies.FORCE);
- startStopsException(StartStrategies.UNREGISTER, StopStrategies.BLOCK);
- startStopsException(StartStrategies.BLOCK, StopStrategies.ASYNCHRONOUS);
- startStopsException(StartStrategies.BLOCK, StopStrategies.SYNCHRONOUS);
- startStopsException(StartStrategies.BLOCK, StopStrategies.FORCE);
- startStopsException(StartStrategies.BLOCK, StopStrategies.BLOCK);
- }
-
- private void startStopsException(StartStrategy startStrategy, StopStrategy stopStrategy) throws Exception {
- serviceFactory.throwExceptionFromCreate = true;
- serviceFactory.throwExceptionFromDestroy = true;
- try {
- start(false, startStrategy);
- } catch (MockCreateException e) {
- assertTrue(startStrategy == StartStrategies.SYNCHRONOUS || startStrategy == StartStrategies.BLOCK);
- assertEquals(serviceFactory.createException, e);
- } catch (UnregisterServiceException e) {
- assertEquals(StartStrategies.UNREGISTER, startStrategy);
- assertEquals(serviceName, e.getServiceName());
- assertSame(serviceFactory.createException, e.getCause());
- }
- stop(stopStrategy);
- }
-
- /**
- * Tests how stop strategies respond when confronted with an unsatisfied stop condition.
- * @throws Exception if a problem occurs
- */
- public void testWaitingStop() throws Exception {
- waitingStop(StopStrategies.ASYNCHRONOUS);
- waitingStop(StopStrategies.SYNCHRONOUS);
- waitingStop(StopStrategies.FORCE);
- }
-
- private void waitingStop(StopStrategy stopStrategy) throws Exception {
- start(false, StartStrategies.SYNCHRONOUS);
- stopCondition.satisfied = false;
- try {
- stop(stopStrategy);
- } catch (UnsatisfiedConditionsException e) {
- assertTrue(stopStrategy == StopStrategies.SYNCHRONOUS);
- assertTrue(e.getUnsatisfiedConditions().contains(stopCondition));
- assertEquals(serviceName, e.getServiceName());
- }
- try {
- stop(stopStrategy);
- } catch (UnsatisfiedConditionsException e) {
- assertTrue(stopStrategy == StopStrategies.SYNCHRONOUS);
- assertTrue(e.getUnsatisfiedConditions().contains(stopCondition));
- assertEquals(serviceName, e.getServiceName());
- }
-
- stopCondition.satisfied = true;
- stop(stopStrategy);
- }
-
- /**
- * Tests the BLOCK stop strategy.
- * @throws Exception if a problem occurs
- */
- public void testBlockStopWaiting() throws Exception {
- start(false, StartStrategies.SYNCHRONOUS);
- stopCondition.satisfied = false;
- stopCondition.isStatisfiedSignal = new CountDownLatch(1);
- FutureTask stopTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- stop(StopStrategies.BLOCK);
- return Boolean.TRUE;
- }
- });
- Thread stopThread = new Thread(stopTask, "StopTask");
- stopThread.setDaemon(true);
- stopThread.start();
-
- // wait for the stop thread to reach the stopContion initialize method
- assertTrue(stopCondition.isStatisfiedSignal.await(5, TimeUnit.SECONDS));
-
- // we should blocked waiting to stop
- assertSame(ServiceState.STOPPING, serviceManager.getState());
- assertTrue(serviceManager.getStartTime() > 0);
- assertSame(SERVICE, serviceManager.getService());
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
- assertTrue(stopCondition.initializeCalled);
- assertTrue(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
-
- // wait for the start task to complete
- stopCondition.satisfied = true;
- stopCondition.context.setSatisfied();
- assertEquals(Boolean.TRUE, stopTask.get(5, TimeUnit.SECONDS));
-
- // we should be STOPPED
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNotNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNotNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopped.getEventId());
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
- assertTrue(stopCondition.initializeCalled);
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- }
-
-
- /**
- * Tests that start throws an exception when the service is in the stoping state.
- * @throws Exception if a problem occurs
- */
- public void testStartFromStopping() throws Exception {
- startFromStopping(StartStrategies.ASYNCHRONOUS);
- startFromStopping(StartStrategies.SYNCHRONOUS);
- startFromStopping(StartStrategies.UNREGISTER);
- startFromStopping(StartStrategies.BLOCK);
- }
-
- private void startFromStopping(StartStrategy startStrategy) throws Exception {
- start(false, StartStrategies.ASYNCHRONOUS);
- stopCondition.satisfied = false;
- stop(StopStrategies.ASYNCHRONOUS);
- try {
- serviceManager.start(false, startStrategy);
- fail("Should have thrown an IllegalServiceStateException since start on a stopping service is an error");
- } catch (IllegalServiceStateException e) {
- // expected
- assertEquals(serviceName, e.getServiceName());
- }
- stopCondition.satisfied = true;
- stop(StopStrategies.ASYNCHRONOUS);
- }
-
- /**
- * Tests that a non-restartable servie is immedately started when initialized and stopped in destroy.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableInitDestroy() throws Exception {
- notRestartableInitDestroy(StopStrategies.ASYNCHRONOUS);
- notRestartableInitDestroy(StopStrategies.SYNCHRONOUS);
- notRestartableInitDestroy(StopStrategies.FORCE);
- notRestartableInitDestroy(StopStrategies.BLOCK);
- }
-
- private void notRestartableInitDestroy(StopStrategy stopStrategy) throws Exception {
- serviceFactory.restartable = false;
- initialize();
- destroy(stopStrategy);
- }
-
- /**
- * Tests how initialize on a non-restartable service responds when an Exception is thrown from create service.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableInitException() throws Exception {
- serviceFactory.throwExceptionFromCreate = true;
- serviceFactory.restartable = false;
- initialize();
- }
-
- /**
- * Tests how initialize on a non-restartable service responds when confronted with an unsatisfied start condition.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableInitWaiting() throws Exception {
- startCondition.satisfied = false;
- serviceFactory.restartable = false;
- initialize();
- }
-
- /**
- * Tests how initialize on a non-restartable service responds when the service factory is disabled.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableInitDisabled() throws Exception {
- serviceFactory.setEnabled(false);
- serviceFactory.restartable = false;
- initialize();
- }
-
- /**
- * Tests how destroy on a non-restartable service responds when destroyService throws an exception.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableDestroyException() throws Exception {
- notRestartableDestroyException(StopStrategies.ASYNCHRONOUS);
- notRestartableDestroyException(StopStrategies.SYNCHRONOUS);
- notRestartableDestroyException(StopStrategies.FORCE);
- notRestartableDestroyException(StopStrategies.BLOCK);
- }
-
- private void notRestartableDestroyException(StopStrategy stopStrategy) throws Exception {
- serviceFactory.throwExceptionFromDestroy = true;
- serviceFactory.restartable = false;
- initialize();
- destroy(stopStrategy);
- }
-
-
- /**
- * Tests how destroy on a non-restartable service responds when createService and destroyService throws an exception.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableInitDestroyException() throws Exception {
- notRestartableInitDestroyException(StopStrategies.ASYNCHRONOUS);
- notRestartableInitDestroyException(StopStrategies.SYNCHRONOUS);
- notRestartableInitDestroyException(StopStrategies.FORCE);
- notRestartableInitDestroyException(StopStrategies.BLOCK);
- }
-
- private void notRestartableInitDestroyException(StopStrategy stopStrategy) throws Exception {
- serviceFactory.throwExceptionFromCreate = true;
- serviceFactory.throwExceptionFromDestroy = true;
- serviceFactory.restartable = false;
- initialize();
- destroy(stopStrategy);
- }
-
- /**
- * Tests how destroy on a non-restartable service responds when confronted with an unsatisfied stop condition.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableWaitingStop() throws Exception {
- notRestartableWaitingDestroy(StopStrategies.ASYNCHRONOUS);
- notRestartableWaitingDestroy(StopStrategies.SYNCHRONOUS);
- notRestartableWaitingDestroy(StopStrategies.FORCE);
- }
-
- private void notRestartableWaitingDestroy(StopStrategy stopStrategy) throws Exception {
- serviceFactory.restartable = false;
- initialize();
- stopCondition.satisfied = false;
- destroy(stopStrategy);
- destroy(stopStrategy);
- stopCondition.satisfied = true;
- destroy(stopStrategy);
- }
-
- /**
- * Tests the BLOCK stop strategy on a non-restartable service.
- * @throws Exception if a problem occurs
- */
- public void testNotRestartableBlockWaitingStop() throws Exception {
- serviceFactory.restartable = false;
- initialize();
- stopCondition.satisfied = false;
- stopCondition.isStatisfiedSignal = new CountDownLatch(1);
- FutureTask destroyTask = new FutureTask(new Callable() {
- public Object call() throws Exception {
- destroy(StopStrategies.BLOCK);
- return Boolean.TRUE;
- }
- });
- Thread destroyThread = new Thread(destroyTask, "DestroyTask");
- destroyThread.setDaemon(true);
- destroyThread.start();
-
- // wait for the stop thread to reach the stopContion initialize method
- assertTrue(stopCondition.isStatisfiedSignal.await(5, TimeUnit.SECONDS));
-
- // we should blocked waiting to stop
- assertSame(ServiceState.RUNNING, serviceManager.getState());
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
- assertTrue(serviceManager.getStartTime() > 0);
- assertNotNull(serviceManager.getService());
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
-
- // wait for the destroy task to complete
- stopCondition.satisfied = true;
- stopCondition.context.setSatisfied();
- assertEquals(Boolean.TRUE, destroyTask.get(5, TimeUnit.SECONDS));
-
- // we should be STOPPED
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNotNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNotNull(serviceMonitor.stopped);
- assertNotNull(serviceMonitor.unregistered);
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopped.getEventId());
- assertEquals(serviceMonitor.stopping.getEventId() + 2, serviceMonitor.unregistered.getEventId());
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
- assertTrue(stopCondition.initializeCalled);
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- }
-
- private void initialize() throws Exception {
- long now = System.currentTimeMillis();
- // introduce a bit of delay so subsequent times are much less likely to equal to not
- Thread.sleep(50);
-
- serviceMonitor.reset();
- startCondition.reset();
- stopCondition.reset();
- kernel.reset();
- try {
- serviceManager.initialize();
- } catch (MockCreateException e) {
- assertTrue(serviceFactory.throwExceptionFromCreate);
- assertSame(serviceFactory.createException, e);
- } catch (UnsatisfiedConditionsException e) {
- assertTrue(!startCondition.satisfied);
- assertTrue(e.getUnsatisfiedConditions().contains(startCondition));
- assertEquals(serviceName, e.getServiceName());
- } catch (IllegalServiceStateException e) {
- assertFalse(serviceFactory.isEnabled());
- assertEquals(serviceName, e.getServiceName());
- }
-
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
-
- if (serviceFactory.restartable) {
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNotNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else if (!serviceFactory.isEnabled()) {
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else if (!serviceFactory.throwExceptionFromCreate && startCondition.satisfied) {
- assertSame(ServiceState.RUNNING, serviceManager.getState());
- assertSame(SERVICE, serviceManager.getService());
- assertTrue(serviceManager.getStartTime() > now);
- assertNotNull(serviceMonitor.registered);
- assertNotNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNotNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
- assertEquals(serviceMonitor.registered.getEventId() + 1, serviceMonitor.starting.getEventId());
- assertEquals(serviceMonitor.registered.getEventId() + 2, serviceMonitor.running.getEventId());
- assertTrue(startCondition.initializeCalled);
- assertTrue(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else {
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNotNull(serviceMonitor.registered);
- assertNotNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNotNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNotNull(serviceMonitor.stopped);
- assertNotNull(serviceMonitor.unregistered);
- assertEquals(serviceMonitor.registered.getEventId() + 1, serviceMonitor.starting.getEventId());
- assertEquals(serviceMonitor.registered.getEventId() + 2, serviceMonitor.stopping.getEventId());
- assertEquals(serviceMonitor.registered.getEventId() + 3, serviceMonitor.stopped.getEventId());
- assertEquals(serviceMonitor.registered.getEventId() + 4, serviceMonitor.unregistered.getEventId());
- assertTrue(startCondition.initializeCalled);
- assertTrue(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- }
- }
-
- private void start(boolean recursive, StartStrategy startStrategy) throws Exception {
- ServiceState initialState = serviceManager.getState();
-
- long now = System.currentTimeMillis();
- // introduce a bit of delay so subsequent times are much less likely to equal to not
- Thread.sleep(50);
-
- serviceMonitor.reset();
- startCondition.reset();
- stopCondition.reset();
- kernel.reset();
- serviceManager.start(recursive, startStrategy);
-
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
-
- // these events should never fire in response to start
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.unregistered);
-
- if (initialState == ServiceState.RUNNING) {
- //
- // We were alredy running so nothing should have happened
- //
- assertSame(ServiceState.RUNNING, serviceManager.getState());
- assertSame(SERVICE, serviceManager.getService());
- assertTrue(serviceManager.getStartTime() > 0);
- assertTrue(serviceManager.getStartTime() <= now);
-
- // verify expected events fired
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
-
- // check if recursive fired
- if (recursive) {
- assertEquals(1, kernel.startRecursive.size());
- assertTrue(kernel.startRecursive.contains(ownedServiceName));
- } else {
- assertTrue(kernel.startRecursive.isEmpty());
- }
-
- // start condition methods
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- // stop condition methods
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else if (!startCondition.satisfied) {
- //
- // watiting to start
- //
- assertTrue(initialState == ServiceState.STOPPED || initialState == ServiceState.STARTING);
-
- // we should have not a service instance and be in the starting state
- assertSame(ServiceState.STARTING, serviceManager.getState());
- assertNull(serviceManager.getService());
- assertEquals(0, serviceManager.getStartTime());
-
-
- // verify expected events fired
- assertNotNull(serviceMonitor.waitingToStart);
-
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
-
- // verify events fired in the correct order
- if (initialState == ServiceState.STOPPED) {
- assertEquals(serviceMonitor.starting.getEventId() + 1, serviceMonitor.waitingToStart.getEventId());
- }
-
- // our condition should be in the unsatisfied condition list
- assertNotNull(serviceMonitor.waitingToStart.getUnsatisfiedConditions());
- assertTrue(serviceMonitor.waitingToStart.getUnsatisfiedConditions().contains(startCondition));
-
- // check if recursive fired
- assertTrue(kernel.startRecursive.isEmpty());
-
- // start condition methods
- if (initialState == ServiceState.STOPPED) {
- assertTrue(startCondition.initializeCalled);
- } else {
- assertFalse(startCondition.initializeCalled);
- }
- assertTrue(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- // stop condition methods
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else if (!serviceFactory.throwExceptionFromCreate) {
- //
- // Normal transition to RUNNING from either STOPPED or STARTING
- //
- assertTrue(initialState == ServiceState.STOPPED || initialState == ServiceState.STARTING);
-
- // we should have a service instance and be in the running state
- assertSame(ServiceState.RUNNING, serviceManager.getState());
- assertSame(SERVICE, serviceManager.getService());
- assertTrue(serviceManager.getStartTime() > now);
-
- if (initialState == ServiceState.STOPPED) {
- assertNotNull(serviceMonitor.starting);
- } else {
- assertNull(serviceMonitor.starting);
- }
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNotNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
-
- // verify events fired in the correct order
- if (initialState == ServiceState.STOPPED) {
- assertEquals(serviceMonitor.starting.getEventId() + 1, serviceMonitor.running.getEventId());
- }
-
- // check if recursive fired
- if (recursive) {
- assertEquals(1, kernel.startRecursive.size());
- assertTrue(kernel.startRecursive.contains(ownedServiceName));
- } else {
- assertTrue(kernel.startRecursive.isEmpty());
- }
-
- // start condition methods
- if (initialState == ServiceState.STOPPED) {
- assertTrue(startCondition.initializeCalled);
- } else {
- assertFalse(startCondition.initializeCalled);
- }
- assertTrue(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- // stop condition methods
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else {
- //
- // Throw an exception from the create method
- //
-
- // we should be stopped
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertNull(serviceManager.getService());
- assertEquals(0, serviceManager.getStartTime());
-
- // verify expected events fired
- assertNotNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNotNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNotNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.stopError);
- assertNotNull(serviceMonitor.stopped);
-
- // verify events fired in the correct order
- assertEquals(serviceMonitor.starting.getEventId() + 1, serviceMonitor.startError.getEventId());
- assertEquals(serviceMonitor.starting.getEventId() + 2, serviceMonitor.stopping.getEventId());
- assertEquals(serviceMonitor.starting.getEventId() + 3, serviceMonitor.stopped.getEventId());
-
- // verify that the exception is in the reson field
- assertSame(serviceFactory.createException, serviceMonitor.startError.getCause());
-
- // check if recursive fired
- assertTrue(kernel.startRecursive.isEmpty());
-
- // start condition methods
- if (initialState == ServiceState.STOPPED) {
- assertTrue(startCondition.initializeCalled);
- } else {
- assertFalse(startCondition.initializeCalled);
- }
- assertTrue(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
-
- // stop condition methods
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- }
- }
-
- private void stop(StopStrategy stopStrategy) throws Exception {
- serviceMonitor.reset();
- startCondition.reset();
- stopCondition.reset();
- kernel.reset();
- ServiceState initialState = serviceManager.getState();
- serviceManager.stop(stopStrategy);
-
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
-
- // these events should never fire in response to start
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
-
- if (initialState == ServiceState.STOPPED) {
- //
- // We were alredy stopped so nothing should have happened
- //
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertSame(ServiceState.STOPPED, serviceManager.getState());
-
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
-
- // start condition methods
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- // stop condition methods
- assertFalse(stopCondition.initializeCalled);
- assertFalse(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else if (!stopCondition.satisfied && stopStrategy != StopStrategies.FORCE) {
- //
- // waiting to stop
- //
- assertSame(ServiceState.STOPPING, serviceManager.getState());
- assertTrue(serviceManager.getStartTime() > 0);
- assertSame(SERVICE, serviceManager.getService());
-
- // verify expected events fired
- if (initialState != ServiceState.STOPPING) {
- assertNotNull(serviceMonitor.stopping);
- }
- assertNotNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
-
- // verify events fired in the correct order
- if (initialState != ServiceState.STOPPING) {
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.waitingToStop.getEventId());
- }
-
- // our condition should be in the unsatisfied condition list
- assertNotNull(serviceMonitor.waitingToStop.getUnsatisfiedConditions());
- assertTrue(serviceMonitor.waitingToStop.getUnsatisfiedConditions().contains(stopCondition));
-
- // start condition methods
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertFalse(startCondition.destroyCalled);
-
- // stop condition methods
- if (initialState == ServiceState.RUNNING) {
- assertTrue(stopCondition.initializeCalled);
- } else {
- assertFalse(stopCondition.initializeCalled);
- }
- assertTrue(stopCondition.isSatisfiedCalled);
- assertFalse(stopCondition.destroyCalled);
- } else if (!serviceFactory.throwExceptionFromDestroy) {
- //
- // Normal transition to STOPPED from either STARTING, RUNNING or STOPPING
- //
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
-
- // verify expected events fired
- if (initialState != ServiceState.STOPPING) {
- assertNotNull(serviceMonitor.stopping);
- }
- assertNull(serviceMonitor.waitingToStop);
- if (stopStrategy == StopStrategies.FORCE) {
- assertNotNull(serviceMonitor.stopError);
- ForcedStopException cause = (ForcedStopException) serviceMonitor.stopError.getCause();
- assertTrue(cause.getUnsatisfiedConditions().contains(stopCondition));
- assertEquals(serviceName, cause.getServiceName());
- } else {
- assertNull(serviceMonitor.stopError);
- }
- assertNotNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
-
- // verify events fired in the correct order
- if (stopStrategy == StopStrategies.FORCE) {
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopError.getEventId());
- assertEquals(serviceMonitor.stopping.getEventId() + 2, serviceMonitor.stopped.getEventId());
- } else if (initialState != ServiceState.STOPPING) {
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopped.getEventId());
- }
-
- // start condition methods
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
-
- // stop condition methods
- if (initialState == ServiceState.STOPPING) {
- assertFalse(stopCondition.initializeCalled);
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- } else {
- assertTrue(stopCondition.initializeCalled);
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- }
- } else {
- //
- // Throw an exception from the destroy method
- //
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertSame(ServiceState.STOPPED, serviceManager.getState());
-
- // verify expected events fired
- if (initialState != ServiceState.STOPPING) {
- assertNotNull(serviceMonitor.stopping);
- }
- assertNull(serviceMonitor.waitingToStop);
- assertNotNull(serviceMonitor.stopError);
- assertNotNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
-
- // verify events fired in the correct order
- if (initialState != ServiceState.STOPPING) {
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopError.getEventId());
- assertEquals(serviceMonitor.stopping.getEventId() + 2, serviceMonitor.stopped.getEventId());
- } else {
- assertEquals(serviceMonitor.stopError.getEventId() + 1, serviceMonitor.stopped.getEventId());
- }
-
- // verify that the exception is in the reson field
- assertSame(serviceFactory.destroyException, serviceMonitor.stopError.getCause());
-
- // start condition methods
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
-
- // stop condition methods
- if (initialState == ServiceState.RUNNING) {
- assertTrue(stopCondition.initializeCalled);
- } else {
- assertFalse(stopCondition.initializeCalled);
- }
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- }
- }
-
- private void destroy(StopStrategy stopStrategy) {
- ServiceState initialState = serviceManager.getState();
- serviceMonitor.reset();
- startCondition.reset();
- stopCondition.reset();
- kernel.reset();
- try {
- serviceManager.destroy(stopStrategy);
- } catch (IllegalServiceStateException e) {
- assertFalse(stopCondition.satisfied);
- assertSame(StopStrategies.ASYNCHRONOUS, stopStrategy);
- assertEquals(serviceName, e.getServiceName());
- } catch (UnsatisfiedConditionsException e) {
- assertFalse(stopCondition.satisfied);
- assertSame(StopStrategies.SYNCHRONOUS, stopStrategy);
- assertTrue(e.getUnsatisfiedConditions().contains(stopCondition));
- assertEquals(serviceName, e.getServiceName());
- }
-
- if (serviceFactory.restartable || initialState == ServiceState.STOPPED) {
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- // verify expected events fired
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNotNull(serviceMonitor.unregistered);
- } else if (!stopCondition.satisfied && stopStrategy != StopStrategies.FORCE) {
- assertSame(ServiceState.RUNNING, serviceManager.getState());
- assertSame(serviceName, serviceManager.getServiceName());
- assertSame(serviceFactory, serviceManager.getServiceFactory());
- assertSame(classLoader, serviceManager.getClassLoader());
- assertTrue(serviceManager.getStartTime() > 0);
- assertNotNull(serviceManager.getService());
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNull(serviceMonitor.stopping);
- if (stopStrategy == StopStrategies.ASYNCHRONOUS) {
- assertNotNull(serviceMonitor.waitingToStop);
- assertTrue(serviceMonitor.waitingToStop.getUnsatisfiedConditions().contains(stopCondition));
- } else {
- assertNull(serviceMonitor.waitingToStop);
- }
- assertNull(serviceMonitor.stopError);
- assertNull(serviceMonitor.stopped);
- assertNull(serviceMonitor.unregistered);
- } else if (!serviceFactory.throwExceptionFromDestroy) {
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNotNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- if (!stopCondition.satisfied && stopStrategy == StopStrategies.FORCE) {
- assertNotNull(serviceMonitor.stopError);
- } else {
- assertNull(serviceMonitor.stopError);
- }
- assertNotNull(serviceMonitor.stopped);
- assertNotNull(serviceMonitor.unregistered);
- if (!stopCondition.satisfied && stopStrategy == StopStrategies.FORCE) {
- assertEquals(serviceMonitor.stopError.getEventId() + 1, serviceMonitor.stopping.getEventId());
- assertEquals(serviceMonitor.stopError.getEventId() + 2, serviceMonitor.stopped.getEventId());
- assertEquals(serviceMonitor.stopError.getEventId() + 3, serviceMonitor.unregistered.getEventId());
- } else {
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopped.getEventId());
- assertEquals(serviceMonitor.stopping.getEventId() + 2, serviceMonitor.unregistered.getEventId());
-
- }
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
- // There is no way to determine if the init should have been called
- // stopCondition.initializeCalled
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- } else {
- assertSame(ServiceState.STOPPED, serviceManager.getState());
- assertEquals(0, serviceManager.getStartTime());
- assertNull(serviceManager.getService());
- assertNull(serviceMonitor.registered);
- assertNull(serviceMonitor.starting);
- assertNull(serviceMonitor.waitingToStart);
- assertNull(serviceMonitor.startError);
- assertNull(serviceMonitor.running);
- assertNotNull(serviceMonitor.stopping);
- assertNull(serviceMonitor.waitingToStop);
- assertNotNull(serviceMonitor.stopError);
- assertNotNull(serviceMonitor.stopped);
- assertNotNull(serviceMonitor.unregistered);
- assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopError.getEventId());
- assertEquals(serviceMonitor.stopping.getEventId() + 2, serviceMonitor.stopped.getEventId());
- assertEquals(serviceMonitor.stopping.getEventId() + 3, serviceMonitor.unregistered.getEventId());
- assertFalse(startCondition.initializeCalled);
- assertFalse(startCondition.isSatisfiedCalled);
- assertTrue(startCondition.destroyCalled);
- assertTrue(stopCondition.initializeCalled);
- assertTrue(stopCondition.isSatisfiedCalled);
- assertTrue(stopCondition.destroyCalled);
- }
- }
-
- protected void setUp() throws Exception {
- super.setUp();
- serviceManager = new ServiceManager(kernel,
- 0,
- serviceName,
- serviceFactory,
- classLoader,
- serviceMonitor,
- 10,
- TimeUnit.SECONDS);
- }
-
- private class MockServiceFactory extends StaticServiceFactory {
- private boolean restartable = true;
- private boolean throwExceptionFromCreate = false;
- private boolean throwExceptionFromDestroy = false;
- private MockCreateException createException;
- private MockDestroyException destroyException;
- private ServiceContext serviceContext;
-
- private MockServiceFactory() throws NullPointerException {
- super(SERVICE);
- }
-
- public Set getOwnedServices() {
- return Collections.singleton(ownedServiceName);
- }
-
- public boolean isRestartable() {
- return restartable;
- }
-
- public synchronized Set getStartConditions() {
- Set startConditions = new HashSet(super.getStartConditions());
- startConditions.add(startCondition);
- return startConditions;
- }
-
- public synchronized Set getStopConditions() {
- Set stopConditions = new HashSet(super.getStopConditions());
- stopConditions.add(stopCondition);
- return stopConditions;
- }
-
- public Object createService(ServiceContext serviceContext) {
- assertValidServiceContext(serviceContext);
- this.serviceContext = serviceContext;
-
- createException = new MockCreateException("MockCreateException");
- if (throwExceptionFromCreate) throw createException;
- return super.createService(serviceContext);
- }
-
- public void destroyService(ServiceContext serviceContext) {
- assertSame(this.serviceContext, serviceContext);
- assertValidServiceContext(serviceContext);
-
- destroyException = new MockDestroyException("MockDestroyException");
- if (throwExceptionFromDestroy) throw destroyException;
- super.destroyService(serviceContext);
- }
-
- private void assertValidServiceContext(ServiceContext serviceContext) {
- assertSame(serviceName, serviceContext.getServiceName());
- assertSame(kernel, serviceContext.getKernel());
- assertSame(classLoader, serviceContext.getClassLoader());
- }
-
- }
-
- private static class MockCreateException extends RuntimeException {
- private MockCreateException(String message) {
- super(message);
- }
- }
-
- private static class MockDestroyException extends RuntimeException {
- private MockDestroyException(String message) {
- super(message);
- }
- }
-
- private class MockStartCondition implements ServiceCondition {
- private boolean satisfied = true;
- private boolean initializeCalled = false;
- private boolean isSatisfiedCalled = false;
- private boolean destroyCalled = false;
- private ServiceConditionContext context;
- private CountDownLatch isSatisfiedSignal;
-
- private void reset() {
- initializeCalled = false;
- isSatisfiedCalled = false;
- destroyCalled = false;
- }
-
- public void initialize(ServiceConditionContext context) {
- assertValidServiceConditionContext(context);
- initializeCalled = true;
- this.context = context;
- }
-
- public boolean isSatisfied() {
- isSatisfiedCalled = true;
- if (isSatisfiedSignal != null) {
- isSatisfiedSignal.countDown();
- }
- return satisfied;
- }
-
- public void destroy() {
- destroyCalled = true;
- }
- }
-
- private class MockStopCondition implements ServiceCondition {
- private boolean satisfied = true;
- private boolean initializeCalled = false;
- private boolean isSatisfiedCalled = false;
- private boolean destroyCalled = false;
- private ServiceConditionContext context;
- private CountDownLatch isStatisfiedSignal;
-
- private void reset() {
- initializeCalled = false;
- isSatisfiedCalled = false;
- destroyCalled = false;
- }
-
- public void initialize(ServiceConditionContext context) {
- assertValidServiceConditionContext(context);
- initializeCalled = true;
- this.context = context;
- }
-
- public boolean isSatisfied() {
- isSatisfiedCalled = true;
- if (isStatisfiedSignal != null) {
- isStatisfiedSignal.countDown();
- }
- return satisfied;
- }
-
- public void destroy() {
- destroyCalled = true;
- }
- }
-
- private void assertValidServiceConditionContext(ServiceConditionContext context) {
- assertSame(serviceName, context.getServiceName());
- assertSame(kernel, context.getKernel());
- assertSame(classLoader, context.getClassLoader());
- }
-
- private class MockServiceMonitor implements ServiceMonitor {
- private ServiceEvent registered;
- private ServiceEvent starting;
- private ServiceEvent waitingToStart;
- private ServiceEvent startError;
- private ServiceEvent running;
- private ServiceEvent stopping;
- private ServiceEvent waitingToStop;
- private ServiceEvent stopError;
- private ServiceEvent stopped;
- private ServiceEvent unregistered;
-
- private void reset() {
- registered = null;
- starting = null;
- waitingToStart = null;
- startError = null;
- running = null;
- stopping = null;
- waitingToStop = null;
- stopError = null;
- stopped = null;
- unregistered = null;
- }
-
- public void serviceRegistered(ServiceEvent serviceEvent) {
- registered = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceStarting(ServiceEvent serviceEvent) {
- starting = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceWaitingToStart(ServiceEvent serviceEvent) {
- waitingToStart = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceStartError(ServiceEvent serviceEvent) {
- startError = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceRunning(ServiceEvent serviceEvent) {
- running = serviceEvent;
- assertValidEvent(serviceEvent, true);
- }
-
- public void serviceStopping(ServiceEvent serviceEvent) {
- stopping = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceWaitingToStop(ServiceEvent serviceEvent) {
- waitingToStop = serviceEvent;
- assertValidEvent(serviceEvent, true);
- }
-
- public void serviceStopError(ServiceEvent serviceEvent) {
- stopError = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceStopped(ServiceEvent serviceEvent) {
- stopped = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- public void serviceUnregistered(ServiceEvent serviceEvent) {
- unregistered = serviceEvent;
- assertValidEvent(serviceEvent, false);
- }
-
- private void assertValidEvent(ServiceEvent serviceEvent, boolean mustHaveService) {
- assertSame(serviceName, serviceEvent.getServiceName());
- assertSame(kernel, serviceEvent.getKernel());
- assertSame(classLoader, serviceEvent.getClassLoader());
- assertSame(serviceFactory, serviceEvent.getServiceFactory());
- if (mustHaveService) {
- assertSame(SERVICE, serviceEvent.getService());
- }
- }
- }
-
- private static class MockKernel implements Kernel {
- private List startRecursive = new LinkedList();
-
- private void reset() {
- startRecursive.clear();
- }
-
- public void startServiceRecursive(ServiceName serviceName) throws ServiceNotFoundException, IllegalStateException {
- startRecursive.add(serviceName);
- }
-
- public void startServiceRecursive(ServiceName serviceName, StartStrategy startStrategy) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- startRecursive.add(serviceName);
- }
-
- //
- // Unimplemented methods
- //
- public void waitForDestruction() {
- throw new UnsupportedOperationException();
- }
-
- public void startService(ServiceName serviceName) throws ServiceNotFoundException, IllegalStateException {
- throw new UnsupportedOperationException();
- }
-
- public void startService(ServiceName serviceName, StartStrategy startStrategy) throws ServiceNotFoundException, IllegalServiceStateException, UnsatisfiedConditionsException, Exception {
- throw new UnsupportedOperationException();
- }
-
- public void stopService(ServiceName serviceName) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public void stopService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, UnsatisfiedConditionsException {
- throw new UnsupportedOperationException();
- }
-
- public void destroy() {
- throw new UnsupportedOperationException();
- }
-
- public boolean isRunning() {
- throw new UnsupportedOperationException();
- }
-
- public String getKernelName() {
- throw new UnsupportedOperationException();
- }
-
- public void registerService(ServiceName serviceName, ServiceFactory serviceFactory, ClassLoader classLoader) throws ServiceAlreadyExistsException, ServiceRegistrationException {
- throw new UnsupportedOperationException();
- }
-
- public void unregisterService(ServiceName serviceName) throws ServiceNotFoundException, IllegalStateException {
- throw new UnsupportedOperationException();
- }
-
- public void unregisterService(ServiceName serviceName, StopStrategy stopStrategy) throws ServiceNotFoundException, ServiceRegistrationException {
- throw new UnsupportedOperationException();
- }
-
- public boolean isRegistered(ServiceName serviceName) {
- throw new UnsupportedOperationException();
- }
-
- public ServiceState getServiceState(ServiceName serviceName) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public long getServiceStartTime(ServiceName serviceName) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public boolean isServiceEnabled(ServiceName serviceName) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public void setServiceEnabled(ServiceName serviceName, boolean enabled) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public Object getService(ServiceName serviceName) throws ServiceNotFoundException, IllegalArgumentException {
- throw new UnsupportedOperationException();
- }
-
- public Object getService(Class type) {
- throw new UnsupportedOperationException();
- }
-
- public List getServices(Class type) {
- throw new UnsupportedOperationException();
- }
-
- public ServiceFactory getServiceFactory(ServiceName serviceName) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public ServiceFactory getServiceFactory(Class type) {
- throw new UnsupportedOperationException();
- }
-
- public List getServiceFactories(Class type) {
- throw new UnsupportedOperationException();
- }
-
- public ClassLoader getClassLoaderFor(ServiceName serviceName) throws ServiceNotFoundException {
- throw new UnsupportedOperationException();
- }
-
- public void addKernelMonitor(KernelMonitor kernelMonitor) {
- throw new UnsupportedOperationException();
- }
-
- public void removeKernelMonitor(KernelMonitor kernelMonitor) {
- throw new UnsupportedOperationException();
- }
-
- public void addServiceMonitor(ServiceMonitor serviceMonitor) {
- throw new UnsupportedOperationException();
- }
-
- public void addServiceMonitor(ServiceMonitor serviceMonitor, ServiceName serviceName) {
- throw new UnsupportedOperationException();
- }
-
- public void removeServiceMonitor(ServiceMonitor serviceMonitor) {
- throw new UnsupportedOperationException();
- }
- }
-}
diff --git a/kernel/src/test/org/xbean/kernel/standard/ServiceMonitorBroadcasterTest.java b/kernel/src/test/org/xbean/kernel/standard/ServiceMonitorBroadcasterTest.java
deleted file mode 100644
index 452eb416..00000000
--- a/kernel/src/test/org/xbean/kernel/standard/ServiceMonitorBroadcasterTest.java
+++ /dev/null
@@ -1,527 +0,0 @@
-/**
- *
- * Copyright 2005 the original author or authors.
- *
- * Licensed 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.xbean.kernel.standard;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.Set;
-
-import junit.framework.TestCase;
-import org.xbean.kernel.KernelErrorsError;
-import org.xbean.kernel.KernelMonitor;
-import org.xbean.kernel.NullServiceMonitor;
-import org.xbean.kernel.ServiceEvent;
-import org.xbean.kernel.ServiceMonitor;
-import org.xbean.kernel.ServiceName;
-import org.xbean.kernel.StaticServiceFactory;
-import org.xbean.kernel.StringServiceName;
-
-/**
- * Tests ServiceMonitorBroadcaster.
- *
- * @author Dain Sundstrom
- * @version $Id$
- * @since 2.0
- */
-public class ServiceMonitorBroadcasterTest extends TestCase {
- private static final StandardKernel KERNEL = new StandardKernel("test");
- private static final StaticServiceFactory SERVICE_FACTORY = new StaticServiceFactory(new Object());
- private static final ClassLoader SYSTEM_CLASS_LOADER = ClassLoader.getSystemClassLoader();
-
- private static final int SERVICE_REGISTERED = 1 << 0;
- private static final int SERVICE_STARTING = 1 << 1;
- private static final int SERVICE_WAITING_TO_START = 1 << 2;
- private static final int SERVICE_START_ERROR = 1 << 3;
- private static final int SERVICE_RUNNING = 1 << 4;
- private static final int SERVICE_STOPPING = 1 << 5;
- private static final int SERVICE_WAITING_TO_STOP = 1 << 6;
- private static final int SERVICE_STOP_ERROR = 1 << 7;
- private static final int SERVICE_STOPPED = 1 << 8;
- private static final int SERVICE_UNREGISTERED = 1 << 9;
- private static final int[] NOTIFICATION_TYPES = new int[]{
- SERVICE_REGISTERED,
- SERVICE_STARTING,
- SERVICE_WAITING_TO_START,
- SERVICE_START_ERROR,
- SERVICE_RUNNING,
- SERVICE_STOPPING,
- SERVICE_WAITING_TO_STOP,
- SERVICE_STOP_ERROR,
- SERVICE_STOPPED,
- SERVICE_UNREGISTERED,
- };
-
-
- private static final int SERVICE_COUNT = 4;
- private static final ServiceName[] serviceName = new ServiceName[SERVICE_COUNT];
- private static final ServiceEvent[] serviceEvent = new ServiceEvent[SERVICE_COUNT];
- private final Set[] expectedMonitors = new Set[SERVICE_COUNT];
- private final Set[] unexpectedMonitors = new Set[SERVICE_COUNT];
-
- private final MockKernelMonitor kernelMonitor = new MockKernelMonitor();
- private final ServiceMonitorBroadcaster serviceMonitorBroadcaster = new ServiceMonitorBroadcaster(kernelMonitor);
- private MockServiceMonitor[] globalMonitors;
- private MockServiceMonitor[][] serviceMonitors;
- private MockServiceMonitor evenMonitor;
- private MockServiceMonitor oddMonitor;
-
- private Throwable throwable;
- private Throwable kernelThrowable;
-
- /**
- * Fires all events on every service and verifies.
- */
- public void testFireEvent() {
- fireAllEvents();
- }
-
- /**
- * Test that if a monitor is added more then once it only recieves the event once.
- * Strategy:
- *
- * Add a monitor several times
- *
- * Fire all events and verify
- *
- */
- public void testMultipleAdd() {
- serviceMonitorBroadcaster.addServiceMonitor(globalMonitors[0], null);
- serviceMonitorBroadcaster.addServiceMonitor(globalMonitors[0], null);
- serviceMonitorBroadcaster.addServiceMonitor(globalMonitors[0], null);
- serviceMonitorBroadcaster.addServiceMonitor(serviceMonitors[1][0], serviceName[1]);
- serviceMonitorBroadcaster.addServiceMonitor(serviceMonitors[1][0], serviceName[1]);
- serviceMonitorBroadcaster.addServiceMonitor(serviceMonitors[1][0], serviceName[1]);
-
- fireAllEvents();
- }
-
- /**
- * Test that events are not fired to a monitor that has been removed.
- * Strategy:
- *
- * Fire all events and verify
- *
- * Removes some monitors
- *
- * Fire all events and verify
- *
- */
- public void testRemove() {
- fireAllEvents();
-
- serviceMonitorBroadcaster.removeServiceMonitor(globalMonitors[0]);
- for (int i = 0; i < expectedMonitors.length; i++) {
- Set expectedMonitor = expectedMonitors[i];
- expectedMonitor.remove(globalMonitors[0]);
- }
- for (int i = 0; i < unexpectedMonitors.length; i++) {
- Set expectedMonitor = unexpectedMonitors[i];
- expectedMonitor.add(globalMonitors[0]);
- }
-
- serviceMonitorBroadcaster.removeServiceMonitor(serviceMonitors[1][0]);
- expectedMonitors[1].remove(serviceMonitors[1][0]);
- unexpectedMonitors[1].add(serviceMonitors[1][0]);
-
- fireAllEvents();
- }
-
- /**
- * Tests that no exceptions are thrown if an attempt is made to remove a monitor not registered.
- */
- public void testRemoveUnassociated() {
- serviceMonitorBroadcaster.removeServiceMonitor(new NullServiceMonitor());
- }
-
- /**
- * Test that when a service monitor throws a RuntimeException the kernel monitor is notified.
- * Strategy:
- *
- * Set all monitors to throw a RuntimeException
- *
- * Fire all events and verify
- *
- */
- public void testNotificationRuntimeException() {
- throwable = new RuntimeException("notification exception");
- fireAllEvents();
- }
-
- /**
- * Test that when a service monitor throws an Error the kernel monitor is notified.
- * Strategy:
- *
- * Set all monitors to throw an Error
- *
- * Fire all events and verify
- *
- */
- public void testNotificationError() {
- throwable = new Error("notification error");
- fireAllEvents();
- }
-
- /**
- * Tests that when a kernel monitor throws a RuntimeException that it is ignored.
- *
- * Set all service monitors to throw a RuntimeException
- *
- * Set kernel monitor to throw a RuntimeException
- *
- * Fire all events and verify
- *
- */
- public void testKernelMonitorException() {
- throwable = new RuntimeException("notification exception");
- kernelThrowable = new RuntimeException("kernel exception");
- fireAllEvents();
- }
-
- /**
- * Tests that when a kernel monitor throws an Error that it is propagated back to call in a KernelErrorsError.
- *
- * Set all service monitors to throw a RuntimeException
- *