protected void createDefenseMissile(String whoLaunchMeId, String targetId, int destructTime) { // must use final for inner thread final int tempDestructTime = destructTime; final String tempLauncherId = whoLaunchMeId, tempTargetId = targetId; Thread temp = new Thread( threadGroup, new Runnable() { @Override public void run() { try { // wait until launch time in XML sleep(tempDestructTime * Utils.SECOND); // update the war if (tempLauncherId.charAt(0) == 'D') { war.interceptGivenMissile(tempLauncherId, tempTargetId); } else war.interceptGivenLauncher(tempLauncherId, tempTargetId); } catch (InterruptedException e) { // System.out.println("The program is close before expected"); // System.out.println(e.getStackTrace()); } } }); temp.start(); }
public void start(ContentHandler sax, Target target) throws IOException, SAXException { this.target = target; XMLReader parser; try { parser = spf.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { throw new LagoonException(e.getMessage()); } parser.setContentHandler(sax); parser.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputSource is = new InputSource(getSourceMan().getFileURL(systemId)); File fil = getSourceMan().getFile(systemId); if (fil != null) { InputStream istr = new FileInputStream(fil); is.setByteStream(istr); } return is; } }); exception = null; mis = new MyInputStream(); mos = new MyOutputStream(mis); thread = new Thread(this); thread.start(); parser.parse(new InputSource(mis)); mis.close(); try { thread.join(1000); } catch (InterruptedException e) { } if (thread.isAlive()) { thread.interrupt(); } this.target = null; if (exception != null) { if (exception instanceof SAXException) { throw (SAXException) exception; } else if (exception instanceof IOException) { throw (IOException) exception; } } }
protected void createEnemyMissile( String missileId, String destination, int launchTime, int flyTime, int damage, String launcherId) { // must use final for inner thread final int tempLaunchTime = launchTime; final int tempDamage = damage; final int tempFlyTime = flyTime; final String tempLauncherId = launcherId; final String tempDestination = destination; final String tempMissileId = missileId; Thread temp = new Thread( threadGroup, new Runnable() { @Override public void run() { try { // wait until launch time in XML sleep(tempLaunchTime * Utils.SECOND); // update the id's IdGenerator.updateEnemyMissileId(tempMissileId); // add to the war war.launchEnemyMissile(tempLauncherId, tempDestination, tempDamage, tempFlyTime); } catch (InterruptedException e) { } } }); temp.start(); }
public String doCommand(String cmd, String args[]) { if (cmd.equals("store")) { cmd += "Expression"; } else if (cmd.equals("assertSelected") || cmd.equals("verifySelected")) { if (args[1].startsWith(INDEX_SPECIFIER)) { cmd += "Index"; args[1] = args[1].substring(INDEX_SPECIFIER.length()); } else if (args[1].startsWith(ID_SPECIFIER)) { cmd += "Id"; args[1] = args[1].substring(ID_SPECIFIER.length()); } else if (args[1].startsWith(LABEL_SPECIFIER)) { cmd += "Label"; args[1] = args[1].substring(LABEL_SPECIFIER.length()); } else if (args[1].startsWith(VALUE_SPECIFIER)) { cmd += "Value"; args[1] = args[1].substring(VALUE_SPECIFIER.length()); } else { cmd += "Label"; } } else if (cmd.endsWith("ErrorOnNext") || cmd.endsWith("FailureOnNext")) { expectError = true; return "OK"; } else if (cmd.equals("echo")) { return "OK," + args[0]; } else if (cmd.equals("pause")) { try { Thread.sleep(Integer.parseInt(args[0])); return "OK"; } catch (InterruptedException e) { return "ERROR: pause interrupted"; } } try { String result = super.doCommand(cmd, args); if (expectError) { throw new SeleniumException("ERROR: Error expected"); } else { return result; } } catch (SeleniumException e) { if (expectError) { expectError = false; return "OK"; } else { throw e; } } }
/** * Associated one DocumentBuilder per thread. This is so we avoid synchronizing (parse() for * example may take a lot of time on a DocumentBuilder) or creating DocumentBuilder instances all * the time. Since typically in an app server we work with a thread pool, not too many instances * of DocumentBuilder should be created. */ private static DocumentBuilder getThreadDocumentBuilder() { Thread thread = Thread.currentThread(); DocumentBuilder documentBuilder = (documentBuilders == null) ? null : documentBuilders.get(thread); // Try a first test outside the synchronized block if (documentBuilder == null) { synchronized (documentBuilderFactory) { // Redo the test within the synchronized block documentBuilder = (documentBuilders == null) ? null : documentBuilders.get(thread); if (documentBuilder == null) { if (documentBuilders == null) documentBuilders = new HashMap<Thread, DocumentBuilder>(); documentBuilder = newDocumentBuilder(); documentBuilders.put(thread, documentBuilder); } } } return documentBuilder; }