2 * Copyright 2007 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.container;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
22 import org.wamblee.system.core.AbstractComponent;
23 import org.wamblee.system.core.DefaultRequiredInterface;
24 import org.wamblee.system.core.ProvidedInterface;
25 import org.wamblee.system.core.RequiredInterface;
26 import org.wamblee.system.core.Scope;
27 import org.wamblee.test.EventTracker;
29 public class Application extends AbstractComponent<Object> {
30 public static RequiredInterface[] required(boolean aOptional,
32 return new RequiredInterface[] {
33 new DefaultRequiredInterface(aPrefix + "string", String.class,
35 new DefaultRequiredInterface(aPrefix + "integer",
36 Integer.class, aOptional) };
39 public static RequiredInterface[] required(boolean aOptional) {
40 return required(aOptional, "");
43 private EventTracker<String> _tracker;
44 private String _string;
45 private Integer _integer;
46 private double _random;
48 public Application() {
52 public Application(String aName) {
56 public Application(String aName, String aPrefix) {
57 super(aName, new ProvidedInterface[0], required(false,
59 _random = Math.random();
62 public Application(boolean aIsOptinal) {
63 super("application", new ProvidedInterface[0], required(true, ""));
66 public Application(EventTracker<String> aTracker) {
72 public Object doStart(Scope aScope) {
73 track("start." + getName());
74 _string = aScope.getInterfaceImplementation(getRequiredInterfaces()
75 .get(0).getProvider(), String.class);
76 _integer = aScope.getInterfaceImplementation(getRequiredInterfaces()
77 .get(1).getProvider(), Integer.class);
81 public String getString() {
85 public Integer getInteger() {
90 public void doStop(Object aRuntime) {
91 track("stop." + getName());
92 if (_random != (Double) aRuntime) {
93 throw new IllegalArgumentException("Wrong runtime: expected "
94 + _random + " but got " + aRuntime);
98 private void track(String aString) {
99 if (_tracker == null) {
102 _tracker.eventOccurred(aString);