2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system.core;
18 import org.wamblee.system.core.AbstractComponent;
19 import org.wamblee.system.core.DefaultProvidedInterface;
20 import org.wamblee.system.core.ProvidedInterface;
21 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.test.EventTracker;
25 import javax.sql.DataSource;
32 public class Environment extends AbstractComponent<Object> {
33 private static int COUNT = 0;
35 private EventTracker<String> tracker;
37 private double random;
42 * Creates a new Environment object.
44 public Environment() {
49 * Creates a new Environment object.
52 public Environment(String aName) {
57 * Creates a new Environment object.
60 public Environment(String aName, String aPrefix) {
61 super(aName, provided(aPrefix), new RequiredInterface[0]);
62 random = Math.random();
67 * Creates a new Environment object.
70 public Environment(EventTracker aTracker) {
75 private static final ProvidedInterface[] provided(String aPrefix) {
76 return new ProvidedInterface[] {
77 new DefaultProvidedInterface(aPrefix + "datasource", String.class),
78 new DefaultProvidedInterface(aPrefix + "integer", Integer.class) };
81 public Integer getInteger() {
85 public String getString() {
86 return getName() + ".hello";
90 protected Object doStart(Scope aScope) {
91 addInterface(getProvidedInterfaces().get(0), getString(), aScope);
92 addInterface(getProvidedInterfaces().get(1), getInteger(), aScope);
93 track("start." + getName());
99 protected void doStop(Object aRuntime) {
100 track("stop." + getName());
102 if (random != (Double) aRuntime) {
103 throw new IllegalArgumentException("Wrong runtime: expected " +
104 random + " but got " + aRuntime);
108 private void track(String aString) {
109 if (tracker == null) {
113 tracker.eventOccurred(aString);