| 1 | package org.jtoolkit.essence.app; |
| 2 | |
| 3 | import org.apache.commons.logging.Log; |
| 4 | import static org.apache.commons.logging.LogFactory.getLog; |
| 5 | import org.jetbrains.annotations.NotNull; |
| 6 | import org.jetbrains.annotations.Nullable; |
| 7 | import org.jtoolkit.essence.app.impl.ContainerImpl; |
| 8 | import org.jtoolkit.essence.app.impl.DynamicMBeanWrapper; |
| 9 | import static org.jtoolkit.essence.utils.StringUtils.isBlank; |
| 10 | |
| 11 | import javax.management.*; |
| 12 | import java.io.IOException; |
| 13 | import static java.lang.management.ManagementFactory.getPlatformMBeanServer; |
| 14 | import static java.net.InetAddress.getLocalHost; |
| 15 | import java.net.UnknownHostException; |
| 16 | import 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 "AS IS" 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 | */ |
| 37 | public 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 | } |