Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol; import java.io.File; import java.net.URL; import java.util.Date; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.PropertyConfigurator; import org.eclipse.core.runtime.AssertionFailedException; import org.eclipse.core.runtime.ILogListener; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import com.clustercontrol.monitor.plugin.IMonitorPlugin; import com.clustercontrol.monitor.plugin.LoadMonitorPlugin; /** * * Hinemos?<BR> * * ???Eclipse(RCP)???? */ public class ClusterControlPlugin extends AbstractUIPlugin { private static Log m_log = LogFactory.getLog(ClusterControlPlugin.class); // ----- static ----- // /** ?? */ public static final String IMG_CONSOLE = "console"; /** ?? */ public static final String IMG_SCOPE = "scope"; /** ?????? */ public static final String IMG_SCOPE_INVALID = "scope_invalid"; /** ?? */ public static final String IMG_NODE = "node"; /** ????? */ public static final String IMG_NODE_INVALID = "node_invalid"; /** ?? */ public static final String IMG_JOBUNIT = "jobunit"; /** ???????? */ public static final String IMG_JOBUNIT_UNREFERABLE = "jobunit_unreferable"; /** ??? */ public static final String IMG_JOBNET = "jobnet"; /** ?? */ public static final String IMG_JOB = "job"; /** ??? */ public static final String IMG_FILEJOB = "filejob"; /** ??? */ public static final String IMG_REFERJOB = "referjob"; /** ???? */ public static final String IMG_REFERJOBNET = "referjobnet"; /** ??? */ public static final String IMG_APPROVALJOB = "approvaljob"; /** ?? */ public static final String IMG_MONITORJOB = "monitorjob"; /** ??? */ public static final String IMG_CHECKED = "checked"; /** ????? */ public static final String IMG_UNCHECKED = "unchecked"; /** ()?? */ public static final String IMG_STATUS_WHITE = "status_white"; /** ()?? */ public static final String IMG_STATUS_YELLOW = "status_yellow"; /** (?)?? */ public static final String IMG_STATUS_BLUE = "status_blue"; /** ()?? */ public static final String IMG_STATUS_RED = "status_red"; /** ()?? */ public static final String IMG_STATUS_GREEN = "status_green"; /** ()?? */ public static final String IMG_END_STATUS_NORMAL = "normal"; /** ()?? */ public static final String IMG_END_STATUS_WARNING = "warning"; /** ()?? */ public static final String IMG_END_STATUS_ABNORMAL = "abnormal"; /** (?)?? */ public static final String IMG_SCHEDULE_PAST = "schedule_past"; /** (?)?? */ public static final String IMG_SCHEDULE_NOW = "schedule_now"; /** (?)?? */ public static final String IMG_SCHEDULE_FUTURE = "schedule_future"; /** ?? */ public static final String IMG_JOB_EDIT_MODE = "job_edit_mode"; /** ??? */ public static final String IMG_ROLESETTING_ROOT = "rootSettingsRoot"; /** ??? */ public static final String IMG_ROLESETTING_ROLE = "rootSettingsRole"; /** ??? */ public static final String IMG_ROLESETTING_USER = "rootSettingsUser"; /** Initial window size and position */ public static final Rectangle WINDOW_INIT_SIZE = new Rectangle(-1, -1, 1024, 768); /** ? */ private final static String DEFAULT_SEPARATOR = ">"; /** The shared instance */ private static ClusterControlPlugin plugin; /** RAP????? */ private static Boolean is_rap = null; /** ?? */ public static final int _intervalMsec = 60000; // ----- instance ----- // //Resource bundle. private ResourceBundle resourceBundle; private String separator; private ILogListener listener; // ----- ----- // /** * The constructor. */ public ClusterControlPlugin() { super(); m_log.debug("ClusterControlPlugin()"); // log4j????? listener = new Listener(); Platform.addLogListener(listener); // ???? String _configFileDir = System.getProperty("hinemos.web.conf.dir"); if (StringUtils.isNotEmpty(_configFileDir)) { String _configFilePath = _configFileDir + File.separator + "log4j.properties"; m_log.info("configFilePath = " + _configFilePath); PropertyConfigurator.configureAndWatch(_configFilePath, _intervalMsec); } setDefault(this); try { resourceBundle = ResourceBundle.getBundle("com.clustercontrol.ClusterControlPluginResources"); } catch (MissingResourceException x) { resourceBundle = null; } // System m_log.info("starting Hinemos Client..."); m_log.info("java.vm.version = " + System.getProperty("java.vm.version")); m_log.info("java.vm.vendor = " + System.getProperty("java.vm.vendor")); m_log.info("java.home = " + System.getProperty("java.home")); m_log.info("os.name = " + System.getProperty("os.name")); m_log.info("os.arch = " + System.getProperty("os.arch")); m_log.info("os.version = " + System.getProperty("os.version")); m_log.info("user.name = " + System.getProperty("user.name")); m_log.info("user.dir = " + System.getProperty("user.dir")); m_log.info("user.country = " + System.getProperty("user.country")); m_log.info("user.language = " + System.getProperty("user.language")); m_log.info("file.encoding = " + System.getProperty("file.encoding")); // long startDate = System.currentTimeMillis(); m_log.info("start date = " + new Date(startDate) + "(" + startDate + ")"); // ??*????????? for (IMonitorPlugin pluginMonitor : LoadMonitorPlugin.getExtensionMonitorList()) { m_log.info("Extended monitor " + pluginMonitor.getMonitorPluginId() + " pluged in."); } } private static class Listener implements ILogListener { @Override public void logging(final IStatus status, final String plugin) { if (status.getSeverity() == IStatus.INFO) { if (status.getException() == null) { m_log.info(status.getMessage()); } else { m_log.info(status.getMessage(), status.getException()); } } else if (status.getSeverity() == IStatus.WARNING) { if (status.getException() == null) { m_log.warn(status.getMessage()); } else { m_log.warn(status.getMessage(), status.getException()); } } else { if (status.getException() == null) { m_log.error(status.getMessage()); } else { m_log.error(status.getMessage(), status.getException()); } } } } // ----- instance ----- // /** * This method is called upon plug-in activation */ @Override public void start(BundleContext context) throws Exception { super.start(context); } /** * This method is called when the plug-in is stopped */ @Override public void stop(BundleContext context) throws Exception { try { super.stop(context); } catch (AssertionFailedException e) { // Web??AssertionFailedException????????? // ????? if (context == null) { m_log.warn("context=" + null); } else { Bundle[] bb = context.getBundles(); for (Bundle b : bb) { m_log.warn("context=" + b.getSymbolicName()); } } } } public static void setDefault(ClusterControlPlugin p) { plugin = p; } /** * Returns the shared instance. */ public static ClusterControlPlugin getDefault() { return plugin; } /** * Returns the string from the plugin's resource bundle, or 'key' if not * found. */ public static String getResourceString(String key) { ResourceBundle bundle = ClusterControlPlugin.getDefault().getResourceBundle(); try { return (bundle != null) ? bundle.getString(key) : key; } catch (MissingResourceException e) { return key; } } /** * Returns the plugin's resource bundle, */ public ResourceBundle getResourceBundle() { return resourceBundle; } /** * ????ImageRegistry???? * * @param registry * ImageRegistry */ @Override protected void initializeImageRegistry(ImageRegistry registry) { this.registerImage(registry, IMG_CONSOLE, "console_view.gif"); this.registerImage(registry, IMG_SCOPE, "scope.gif"); this.registerImage(registry, IMG_SCOPE_INVALID, "scope_invalid.gif"); this.registerImage(registry, IMG_NODE, "node.gif"); this.registerImage(registry, IMG_NODE_INVALID, "node_invalid.gif"); this.registerImage(registry, IMG_JOBUNIT, "job_unit.gif"); this.registerImage(registry, IMG_JOBUNIT_UNREFERABLE, "job_unit_unreferable.gif"); this.registerImage(registry, IMG_JOBNET, "job_net.gif"); this.registerImage(registry, IMG_JOB, "job.gif"); this.registerImage(registry, IMG_FILEJOB, "file_obj.gif"); this.registerImage(registry, IMG_REFERJOB, "refer_job.gif"); this.registerImage(registry, IMG_REFERJOBNET, "refer_job_net.gif"); this.registerImage(registry, IMG_APPROVALJOB, "approval_job.gif"); this.registerImage(registry, IMG_MONITORJOB, "monitor_job.gif"); this.registerImage(registry, IMG_CHECKED, "checked.gif"); this.registerImage(registry, IMG_UNCHECKED, "unchecked.gif"); this.registerImage(registry, IMG_STATUS_BLUE, "status_blue.gif"); this.registerImage(registry, IMG_STATUS_GREEN, "status_green.gif"); this.registerImage(registry, IMG_STATUS_RED, "status_red.gif"); this.registerImage(registry, IMG_STATUS_WHITE, "status_white.gif"); this.registerImage(registry, IMG_STATUS_YELLOW, "status_yellow.gif"); this.registerImage(registry, IMG_END_STATUS_NORMAL, "normal.gif"); this.registerImage(registry, IMG_END_STATUS_WARNING, "warning.gif"); this.registerImage(registry, IMG_END_STATUS_ABNORMAL, "abnormal.gif"); this.registerImage(registry, IMG_SCHEDULE_PAST, "schedule_g.gif"); this.registerImage(registry, IMG_SCHEDULE_NOW, "schedule_b.gif"); this.registerImage(registry, IMG_SCHEDULE_FUTURE, "schedule_w.gif"); this.registerImage(registry, IMG_JOB_EDIT_MODE, "job_edit_mode.gif"); /* * RoleSettingTree */ this.registerImage(registry, IMG_ROLESETTING_ROOT, "node.gif"); this.registerImage(registry, IMG_ROLESETTING_ROLE, "role.gif"); this.registerImage(registry, IMG_ROLESETTING_USER, "user.gif"); } /** * ImageRegistry???? * * @param registry * ImageRegistry * @param key * ? * @param fileName * ?? */ private void registerImage(ImageRegistry registry, String key, String fileName) { try { URL url = new URL(getDefault().getBundle().getEntry("/"), "icons/" + fileName); ImageDescriptor desc = ImageDescriptor.createFromURL(url); registry.put(key, desc); } catch (Exception e) { m_log.debug(e.getMessage(), e); } } /** * @return Returns the separator. */ public String getSeparator() { if (separator == null || separator.compareTo("") == 0) { separator = DEFAULT_SEPARATOR; } return separator; } /** * @param separator * The separator to set. */ public void setSeparator(String separator) { this.separator = separator; } /** * ID????????ID??? * * @return ID * @see Bundle#getSymbolicName() */ public static String getPluginId() { return getDefault().getBundle().getSymbolicName(); } /** * Check if this application is run in RAP * @return Is RAP or not */ public static Boolean isRAP() { if (null == is_rap) { // getAppName() will return null in RAP is_rap = (null == Display.getAppName()); } return is_rap; } /** * ???????? */ public static Boolean isExitConfirm() { String exitConfrim = System.getProperty("exit.confirm"); if (exitConfrim != null && exitConfrim.equals("false")) { return false; } return true; } }