EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.app]

COVERAGE SUMMARY FOR SOURCE FILE [Main.java]

nameclass, %method, %block, %line, %
Main.java100% (2/2)80%  (12/15)57%  (247/432)72%  (65.6/91)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Main100% (1/1)77%  (10/13)57%  (242/427)71%  (62.6/88)
Main (): void 0%   (0/1)0%   (0/3)0%   (0/2)
getThreadName (): String 0%   (0/1)0%   (0/3)0%   (0/1)
main (String []): void 0%   (0/1)0%   (0/44)0%   (0/7)
unregisterComponent (ObjectName): void 100% (1/1)37%  (15/41)73%  (7.3/10)
getServerName (): String 100% (1/1)41%  (15/37)49%  (3.9/8)
registerComponent (String, Object, String): ObjectInstance 100% (1/1)44%  (32/73)68%  (9.6/14)
start (String, String): Container 100% (1/1)55%  (51/93)84%  (11.8/14)
closeAll0 (): void 100% (1/1)80%  (16/20)67%  (4/6)
<static initializer> 100% (1/1)100% (24/24)100% (7/7)
access$000 (): void 100% (1/1)100% (2/2)100% (1/1)
closeAll (): void 100% (1/1)100% (4/4)100% (3/3)
generateContexts (String): String [] 100% (1/1)100% (77/77)100% (13/13)
unregisterComponent (ObjectInstance): void 100% (1/1)100% (6/6)100% (2/2)
     
class Main$1100% (1/1)100% (2/2)100% (5/5)100% (3/3)
Main$1 (): void 100% (1/1)100% (3/3)100% (1/1)
run (): void 100% (1/1)100% (2/2)100% (2/2)

1package org.jtoolkit.essence.app;
2 
3import org.apache.commons.logging.Log;
4import static org.apache.commons.logging.LogFactory.getLog;
5import org.jetbrains.annotations.NotNull;
6import org.jetbrains.annotations.Nullable;
7import org.jtoolkit.essence.app.impl.ContainerImpl;
8import org.jtoolkit.essence.app.impl.DynamicMBeanWrapper;
9import static org.jtoolkit.essence.utils.StringUtils.isBlank;
10 
11import javax.management.*;
12import java.io.IOException;
13import static java.lang.management.ManagementFactory.getPlatformMBeanServer;
14import static java.net.InetAddress.getLocalHost;
15import java.net.UnknownHostException;
16import java.util.*;
17 
18/*
19   Copyright 2006 Peter Lawrey
20 
21   Licensed under the Apache License, Version 2.0 (the "License");
22   you may not use this file except in compliance with the License.
23   You may obtain a copy of the License at
24 
25       http://www.apache.org/licenses/LICENSE-2.0
26 
27   Unless required by applicable law or agreed to in writing, software
28   distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
29   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30   See the License for the specific language governing permissions and
31   limitations under the License.
32*/
33 
34/**
35 * @author Peter Lawrey
36 */
37public class Main {
38    private static final Log LOG = getLog(Main.class);
39    private static final Map<String, Container> containers = new LinkedHashMap<String, Container>();
40    public static final String SERVER_NAME = getServerName();
41    public static final String LOCALHOST = "localhost";
42    public static final String JAVA_LANG_REFLECT = "java.lang.reflect.";
43    public static final String JAVA_UTIL_CONCURRENT = "java.util.concurrent.";
44    public static final String JUNIT = "junit.";
45    public static final String COM_INTELLIJ_RT_EXECUTION = "com.intellij.rt.execution.";
46    public static final String SUN_REFLECT = "sun.reflect.";
47    public static final String UNHANDLED_EXCEPTION = "Unhandled exception";
48 
49    private Main() {
50    }
51 
52    static {
53        Thread hook = new Thread(new Runnable() {
54            public void run() {
55                closeAll0();
56            }
57        }, "stop-containers");
58        // noinspection CallToThreadSetPriority
59        hook.setPriority(Thread.MAX_PRIORITY);
60        Runtime.getRuntime().addShutdownHook(hook);
61    }
62 
63    public static synchronized void closeAll() {
64        closeAll0();
65        containers.clear();
66    }
67 
68    private static void closeAll0() {
69        for (Container container : containers.values()) {
70            try {
71                container.close();
72            } catch (Exception e) {
73                LOG.error(e);
74            }
75        }
76    }
77 
78    @NotNull
79    public static Container start(@NotNull String name, @NotNull String url) throws IOException, InstantiationException {
80        Container container;
81        synchronized (Main.class) {
82            container = containers.get(name);
83            if (container != null && !container.isClosed()) {
84                if (container.getUrl().equals(url))
85                    return container;
86                throw new IllegalArgumentException("Attempt to load container "+name+" with url "+url+" when is already running from url "+container.getUrl());
87            }
88 
89            containers.put(name, container = new ContainerImpl(name, url));
90 
91            container.call(Runs.START, true);
92        }
93        // noinspection CallToThreadYield
94        Thread.yield();
95 
96        Set<String> unusedConfig = container.remainingConfig();
97        if (!unusedConfig.isEmpty())
98            LOG.warn(name + ": Configuration sections not used. " + unusedConfig + " from " + url);
99 
100        return container;
101    }
102 
103    @SuppressWarnings({"UseOfSystemOutOrSystemErr"})
104    public static void main(String... args) throws IOException, InstantiationException {
105        if (args.length < 1 || args.length > 2) {
106            System.err.println("Usage: java -cp essence.jar " + Main.class.getName() + " {config-file} [{application-name}]");
107            System.exit(1);
108        }
109        String app = args[0];
110        String file = args.length > 1 ? args[1] : args[0];
111        start(app, file);
112    }
113 
114    @Nullable
115    public static ObjectInstance registerComponent(@NotNull String name, @NotNull Object component, @Nullable String description) {
116        MBeanServer bs = getPlatformMBeanServer();
117        ObjectName oname = null;
118        try {
119            try {
120                oname = new ObjectName(name);
121                if (LOG.isDebugEnabled()) LOG.debug(getThreadName() + ":: Registering " + name + ' ' + description);
122                return bs.registerMBean(component, oname);
123            } catch (NotCompliantMBeanException ignore) {
124                if (isBlank(description)) description = "{none}";
125                return bs.registerMBean(new DynamicMBeanWrapper<Object>(component, description), oname);
126            }
127        } catch (InstanceAlreadyExistsException e) {
128            if (LOG.isDebugEnabled()) LOG.debug("Instance already registered", e);
129        } catch (Exception e) {
130            LOG.warn(getThreadName() + ": Unable to register " + name, e);
131        }
132        return null;
133    }
134 
135    private static String getThreadName() {
136        return Thread.currentThread().getName();
137    }
138 
139    public static void unregisterComponent(@Nullable ObjectInstance objectInstance) {
140        if (objectInstance != null) unregisterComponent(objectInstance.getObjectName());
141    }
142 
143    public static void unregisterComponent(@Nullable ObjectName objectName) {
144        if (objectName == null)
145            return;
146        MBeanServer bs = getPlatformMBeanServer();
147        try {
148            if (LOG.isDebugEnabled()) LOG.debug(getThreadName() + ": Unregistering " + objectName);
149            bs.unregisterMBean(objectName);
150        } catch (InstanceNotFoundException ignored) {
151            // ignored
152        } catch (MBeanRegistrationException e) {
153            LOG.error(objectName + ": Unable to unregister " + objectName, e);
154        }
155    }
156 
157    public static String[] generateContexts(String name) {
158        String[] parts = name.split("\\.");
159        List<String> ret = new ArrayList<String>();
160        for (int i = (1 << parts.length) - 1; i >= 0; i--) {
161            StringBuilder sb = new StringBuilder();
162            int mask = 1 << parts.length - 1;
163            for (String part : parts) {
164                if ((i & mask) != 0) {
165                    if (sb.length() > 0)
166                        sb.append('.');
167                    sb.append(part);
168                }
169                mask >>= 1;
170            }
171            ret.add(sb.toString());
172        }
173 
174        return ret.toArray(new String[ret.size()]);
175    }
176 
177    private static String getServerName() {
178        try {
179            String serverHostname = getLocalHost().getHostName().toLowerCase();
180            int pos = serverHostname.indexOf('.');
181            if (pos > 0) serverHostname = serverHostname.substring(0, pos);
182            if (LOG.isDebugEnabled()) LOG.debug("Hostname " + serverHostname);
183            return serverHostname;
184        } catch (UnknownHostException e) {
185            LOG.warn("Unable to determine the hostname, assuming localhost.", e);
186            return LOCALHOST;
187        }
188    }
189}

[all classes][org.jtoolkit.essence.app]
EMMA 2.0.5312 (C) Vladimir Roubtsov