📄 q2.java
字号:
try { modified = ( (Boolean) server.getAttribute (name, "Modified") ).booleanValue(); } catch (Exception e) { // Okay to fail } } return modified; } private long persist (File f, ObjectName name) { long deployed = f.lastModified (); try { Element e = (Element) server.getAttribute (name, "Persist"); if (e != null) { XMLOutputter out = new XMLOutputter (Format.getPrettyFormat()); Document doc = new Document (); e.detach(); doc.setRootElement (e); File tmp = new File (f.getAbsolutePath () + ".tmp"); FileWriter writer = new FileWriter (tmp); out.output (doc, writer); writer.close (); f.delete(); tmp.renameTo (f); deployed = f.lastModified (); } } catch (Exception ex) { log.warn ("persist", ex); } return deployed; } private void undeploy (File f) { QEntry qentry = (QEntry) dirMap.get (f); try { if (log != null) log.trace ("undeploying:" + f.getCanonicalPath()); Object obj = qentry.getObject (); ObjectName name = qentry.getObjectName (); factory.destroyQBean (this, name, obj); if (log != null) log.info ("undeployed:" + f.getCanonicalPath()); } catch (Exception e) { getLog().warn ("undeploy", e); } } private boolean register (File f) { boolean rc = false; if (f.isDirectory()) { File file[] = f.listFiles (this); for (int i=0; i<file.length; i++) { if (register (file[i])) rc = true; } } else if (dirMap.get (f) == null) { dirMap.put (f, new QEntry ()); rc = true; } return rc; } private boolean deploy (File f) { try { if (log != null) log.info ("deploy:" + f.getCanonicalPath()); QEntry qentry = (QEntry) dirMap.get (f); SAXBuilder builder = new SAXBuilder (); Document doc = decrypt (builder.build (f)); Object obj = factory.instantiate (this, doc.getRootElement ()); qentry.setObject (obj); ObjectInstance instance = factory.createQBean ( this, doc.getRootElement(), obj ); qentry.setInstance (instance); } catch (InstanceAlreadyExistsException e) { /* * Ok, the file we tried to deploy, holds an object * that already has been deployed. * * Rename it out of the way. * */ tidyFileAway(f,DUPLICATE_EXTENSION); getLog().warn ("deploy", e); return false; } catch (Exception e) { getLog().warn ("deploy", e); tidyFileAway(f,ERROR_EXTENSION); // This will also save deploy error repeats... return false; } catch (Error e) { getLog().warn ("deploy", e); tidyFileAway(f,ENV_EXTENSION); // This will also save deploy error repeats... return false; } return true ; } private void start (ObjectInstance instance) { try { factory.startQBean (this, instance.getObjectName()); } catch (Exception e) { getLog().warn ("start", e); } } public void relax (long sleep) { try { Thread.sleep (sleep); } catch (InterruptedException e) { } } public void relax () { relax (1000); } private void initSystemLogger () { File loggerConfig = new File (deployDir, LOGGER_CONFIG); if (loggerConfig.canRead()) { hasSystemLogger = true; try { register (loggerConfig); deploy (); } catch (Exception e) { getLog().warn ("init-system-logger", e); } } getLog().info ("Q2 started, deployDir="+deployDir.getAbsolutePath()); } public Log getLog () { if (log == null) { Logger logger = Logger.getLogger (LOGGER_NAME); if (!hasSystemLogger && !logger.hasListeners()) logger.addListener (new SimpleLogListener (System.out)); log = new Log (logger, REALM); } return log; } public MBeanServer getMBeanServer () { return server; } public long getUptime() { return System.currentTimeMillis() - startTime; } private void parseCmdLine (String[] args) { CommandLineParser parser = new PosixParser (); Options options = new Options (); options.addOption ("v","version", false, "Q2's version"); options.addOption ("d","deploydir", true, "Deployment directory"); options.addOption ("r","recursive", false, "Deploy subdirectories recursively"); options.addOption ("h","help", false, "Usage information"); options.addOption ("C","config", true, "Configuration bundle"); options.addOption ("e","encrypt", true, "Encrypt configuration bundle"); options.addOption ("i","cli", false, "Command Line Interface"); options.addOption ("c","command", true, "Command to execute"); try { CommandLine line = parser.parse (options, args); if (line.hasOption ("v")) { System.out.println ( "Q2 version: " +Q2_VERSION + "(" + Q2_REVISION + ")" ); System.exit (0); } if (line.hasOption ("h")) { HelpFormatter helpFormatter = new HelpFormatter (); helpFormatter.printHelp ("Q2", options); System.exit (0); } if (line.hasOption ("c")) { cli = new CLI(this, line.getOptionValue("c"), line.hasOption("i")); } else if (line.hasOption ("i")) cli = new CLI(this, null, true); String dir = DEFAULT_DEPLOY_DIR; if (line.hasOption ("d")) { dir = line.getOptionValue ("d"); } recursive = line.hasOption ("r"); this.deployDir = new File (dir); if (line.hasOption ("C")) deployBundle (new File (line.getOptionValue ("C")), false); if (line.hasOption ("e")) deployBundle (new File (line.getOptionValue ("e")), true); } catch (MissingArgumentException e) { System.out.println ("ERROR: " + e.getMessage()); System.exit (1); } catch (Exception e) { e.printStackTrace (); System.exit (1); } } private void deployBundle (File bundle, boolean encrypt) throws JDOMException, IOException, ISOException, GeneralSecurityException { SAXBuilder builder = new SAXBuilder (); Document doc = builder.build (bundle); Iterator iter = doc.getRootElement().getChildren ().iterator (); for (int i=0; iter.hasNext (); i += 5) { deployElement ((Element) iter.next (), i, encrypt); } } private void deployElement (Element e, int i, boolean encrypt) throws ISOException, IOException, GeneralSecurityException { e = ((Element) e.clone ()); XMLOutputter out = new XMLOutputter (Format.getPrettyFormat()); Document doc = new Document (); doc.setRootElement (e); File qbean = new File ( deployDir, ISOUtil.zeropad (Integer.toString (i),3) + "_" + e.getName () + ".xml" ); FileWriter writer = new FileWriter (qbean); if (encrypt) doc = encrypt (doc); out.output (doc, writer); writer.close (); } private byte[] dodes (byte[] data, int mode) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance ("DES"); cipher.init (mode, new SecretKeySpec(getKey(), "DES")); return cipher.doFinal (data); } protected byte[] getKey() { return "CAFEBABE".getBytes(); } protected Document encrypt (Document doc) throws GeneralSecurityException, IOException { ByteArrayOutputStream os = new ByteArrayOutputStream (); OutputStreamWriter writer = new OutputStreamWriter (os); XMLOutputter out = new XMLOutputter (Format.getPrettyFormat()); out.output (doc, writer); writer.close (); byte[] crypt = dodes (os.toByteArray(), Cipher.ENCRYPT_MODE); Document secureDoc = new Document (); Element root = new Element (PROTECTED_QBEAN); secureDoc.setRootElement (root); Element secureData = new Element ("data"); root.addContent (secureData); secureData.setText ( ISOUtil.hexString (crypt) ); return secureDoc; } protected Document decrypt (Document doc) throws GeneralSecurityException, IOException, JDOMException { Element root = doc.getRootElement (); if (PROTECTED_QBEAN.equals (root.getName ())) { Element data = root.getChild ("data"); if (data != null) { ByteArrayInputStream is = new ByteArrayInputStream ( dodes ( ISOUtil.hex2byte (data.getTextTrim()), Cipher.DECRYPT_MODE) ); SAXBuilder builder = new SAXBuilder (); doc = builder.build (is); } } return doc; } private void tidyFileAway (File f, String extension) { File rename = new File(f.getAbsolutePath()+"."+extension); while (rename.exists()){ rename = new File(rename.getAbsolutePath()+"."+extension); } getLog().warn("Tidying "+f.getAbsolutePath()+" out of the way, by adding ."+extension,"It will be called: "+rename.getAbsolutePath()+" see log above for detail of problem."); f.renameTo(rename); } private void setExit (boolean exit) { this.exit = exit; } public static void main (String[] args) throws Exception { Q2 q2 = new Q2(args); q2.setExit (true); q2.start(); } public String getVersion() { return Q2_VERSION; } public String getRevision() { return Q2_REVISION; } public String getDate() { return Q2_DATE; } public String getRelease() { return getVersion() + getRevision(); } public static class QEntry { long deployed; ObjectInstance instance; Object obj; public QEntry () { super(); } public QEntry (long deployed, ObjectInstance instance) { super(); this.deployed = deployed; this.instance = instance; } public long getDeployed () { return deployed; } public void setDeployed (long deployed) { this.deployed = deployed; } public void setInstance (ObjectInstance instance) { this.instance = instance; } public ObjectInstance getInstance () { return instance; } public ObjectName getObjectName () { return instance != null ? instance.getObjectName () : null; } public void setObject (Object obj) { this.obj = obj; } public Object getObject () { return obj; } public boolean isQBean () { return obj instanceof QBean; } public boolean isQPersist () { return obj instanceof QPersist; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -