/** * @param guid * @throws IOException */ public void delete(int guid) throws IOException { // delete file File file = new File(Integer.toString(guid)); if (file.delete()) { System.out.println(file.getName() + " deleted"); } else { System.out.println("Failed to delete " + file.getName()); } }
public int start() throws RemoteException { try { File logF = new File(logFile); FileWriter fw = new FileWriter(logF.getAbsoluteFile(), true); bw = new BufferedWriter(fw); bw.write(Integer.toString(xidCounter)); bw.write(" started\n"); bw.close(); } catch (IOException e) { e.printStackTrace(); } enlistList.put(xidCounter, new HashSet<ResourceManager>()); protocolDB.put(xidCounter, new HashMap<String, TwoPC_ST>()); return xidCounter++; }
public TransactionManagerImpl() throws RemoteException, TransactionAbortedException, InvalidTransactionException { File logF = new File(logFile); try { if (!logF.exists()) { logF.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } if (!(new File("data").mkdir())) { System.err.println("mkdir data not successful"); } dieTMBeforeCommit = false; dieTMAfterCommit = false; }
/** Call invokeSlave with the appropriate JVMBuilder. */ private void _doStartup() { File dir = _workingDir; // TODO: Eliminate NULL_FILE. It is a bad idea! The correct behavior when it is used always // depends on // context, so it can never be treated transparently. In this case, the process won't start. if (dir == FileOps.NULL_FILE) { dir = IOUtil.WORKING_DIRECTORY; } List<String> jvmArgs = new ArrayList<String>(); // ConcJUnit argument: -Xbootclasspath/p:rt.concjunit.jar // ------------------------------------------------------ // this section here loops if the rt.concjunit.jar file is // being re-generated or the settings are changed final CompletionMonitor cm = new CompletionMonitor(); boolean repeat; do { repeat = false; File junitLocation = DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION); boolean javaVersion7 = JavaVersion.CURRENT.supports(JavaVersion.JAVA_7); // ConcJUnit is available if (a) the built-in framework is used, or (b) the external // framework is a valid ConcJUnit jar file, AND the compiler is not Java 7 or newer. boolean concJUnitAvailable = !javaVersion7 && (!DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION_ENABLED) || edu.rice.cs.drjava.model.junit.ConcJUnitUtils.isValidConcJUnitFile( junitLocation)); File rtLocation = DrJava.getConfig().getSetting(OptionConstants.RT_CONCJUNIT_LOCATION); boolean rtLocationConfigured = edu.rice.cs.drjava.model.junit.ConcJUnitUtils.isValidRTConcJUnitFile(rtLocation); if (DrJava.getConfig() .getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED) .equals(OptionConstants.ConcJUnitCheckChoices.ALL) && // "lucky" enabled !rtLocationConfigured && // not valid (rtLocation != null) && // not null (!FileOps.NULL_FILE.equals(rtLocation)) && // not NULL_FILE (rtLocation.exists())) { // but exists // invalid file, clear setting DrJava.getConfig() .setSetting( OptionConstants.CONCJUNIT_CHECKS_ENABLED, OptionConstants.ConcJUnitCheckChoices.NO_LUCKY); rtLocationConfigured = false; javax.swing.JOptionPane.showMessageDialog( null, "The selected file is invalid and was disabled:\n" + rtLocation, "Invalid ConcJUnit Runtime File", javax.swing.JOptionPane.ERROR_MESSAGE); } if (concJUnitAvailable && // ConcJUnit available rtLocationConfigured && // runtime configured DrJava.getConfig() .getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED) .equals(OptionConstants.ConcJUnitCheckChoices.ALL)) { // and "lucky" enabled try { // NOTE: this is a work-around // it seems like it's impossible to pass long file names here on Windows // so we are using a clumsy method that determines the short file name File shortF = FileOps.getShortFile(rtLocation); // check the JavaVersion of the rt.concjunit.jar file to make sure it is compatible if (edu.rice.cs.drjava.model.junit.ConcJUnitUtils.isCompatibleRTConcJUnitFile(shortF)) { // enabled, valid and compatible // add the JVM argument jvmArgs.add( "-Xbootclasspath/p:" + shortF.getAbsolutePath().replace(File.separatorChar, '/')); } else { // enabled, valid but incompatible // ask to regenerate repeat = true; // re-check settings cm.reset(); boolean attempted = edu.rice.cs.drjava.model.junit.ConcJUnitUtils .showIncompatibleWantToRegenerateDialog( null, new Runnable() { public void run() { cm.signal(); } }, // yes new Runnable() { public void run() { cm.signal(); } }); // no while (!cm.attemptEnsureSignaled()) ; // wait for dialog to finish if (!attempted) { repeat = false; } } } catch (IOException ioe) { // we couldn't get the short file name (on Windows), disable "lucky" warnings DrJava.getConfig() .setSetting( OptionConstants.CONCJUNIT_CHECKS_ENABLED, OptionConstants.ConcJUnitCheckChoices.NO_LUCKY); rtLocationConfigured = false; javax.swing.JOptionPane.showMessageDialog( null, "There was a problem with the selected file, and it was disabled:\n" + rtLocation, "Invalid ConcJUnit Runtime File", javax.swing.JOptionPane.ERROR_MESSAGE); } } } while (repeat); // end of the section that may loop // ------------------------------------------------------ if (_allowAssertions) { jvmArgs.add("-ea"); } int debugPort = _getDebugPort(); if (debugPort > -1) { jvmArgs.add("-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=" + debugPort); jvmArgs.add("-Xdebug"); jvmArgs.add("-Xnoagent"); jvmArgs.add("-Djava.compiler=NONE"); } String slaveMemory = DrJava.getConfig().getSetting(OptionConstants.SLAVE_JVM_XMX); if (!"".equals(slaveMemory) && !OptionConstants.heapSizeChoices.get(0).equals(slaveMemory)) { jvmArgs.add("-Xmx" + slaveMemory + "M"); } String slaveArgs = DrJava.getConfig().getSetting(OptionConstants.SLAVE_JVM_ARGS); if (PlatformFactory.ONLY.isMacPlatform()) { jvmArgs.add("-Xdock:name=Interactions"); } // add additional boot class path items specified by the selected compiler for (File f : _interactionsModel.getCompilerBootClassPath()) { try { // NOTE: this is a work-around // it seems like it's impossible to pass long file names here on Windows // so we are using a clumsy method that determines the short file name File shortF = FileOps.getShortFile(f); jvmArgs.add( "-Xbootclasspath/a:" + shortF.getAbsolutePath().replace(File.separatorChar, '/')); } catch (IOException ioe) { // TODO: figure out what to do here. error? warn? } } jvmArgs.addAll(ArgumentTokenizer.tokenize(slaveArgs)); JVMBuilder jvmb = new JVMBuilder(_startupClassPath).directory(dir).jvmArguments(jvmArgs); // extend classpath if JUnit/ConcJUnit location specified File junitLocation = DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION); boolean junitLocationConfigured = (edu.rice.cs.drjava.model.junit.ConcJUnitUtils.isValidJUnitFile(junitLocation) || edu.rice.cs.drjava.model.junit.ConcJUnitUtils.isValidConcJUnitFile(junitLocation)); if (DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION_ENABLED) && // enabled !junitLocationConfigured && // not valid (junitLocation != null) && // not null (!FileOps.NULL_FILE.equals(junitLocation)) && // not NULL_FILE (junitLocation.exists())) { // but exists // invalid file, clear setting DrJava.getConfig().setSetting(OptionConstants.JUNIT_LOCATION_ENABLED, false); junitLocationConfigured = false; } ArrayList<File> extendedClassPath = new ArrayList<File>(); if (DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION_ENABLED) && junitLocationConfigured) { extendedClassPath.add(junitLocation); } for (File f : jvmb.classPath()) { extendedClassPath.add(f); } jvmb = jvmb.classPath(edu.rice.cs.plt.iter.IterUtil.asSizedIterable(extendedClassPath)); // add Java properties controlling ConcJUnit Map<String, String> props = jvmb.propertiesCopy(); // settings are mutually exclusive boolean all = DrJava.getConfig() .getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED) .equals(OptionConstants.ConcJUnitCheckChoices.ALL); boolean noLucky = DrJava.getConfig() .getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED) .equals(OptionConstants.ConcJUnitCheckChoices.NO_LUCKY); boolean none = DrJava.getConfig() .getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED) .equals(OptionConstants.ConcJUnitCheckChoices.NONE); // "threads" is enabled as long as the setting isn't NONE props.put("edu.rice.cs.cunit.concJUnit.check.threads.enabled", new Boolean(!none).toString()); // "join" is enabled for ALL and NO_LUCKY props.put( "edu.rice.cs.cunit.concJUnit.check.join.enabled", new Boolean(all || noLucky).toString()); // "lucky" is enabled only for ALL props.put("edu.rice.cs.cunit.concJUnit.check.lucky.enabled", new Boolean(all).toString()); jvmb = jvmb.properties(props); invokeSlave(jvmb); }
public static void main(String args[]) throws RemoteException, InvalidTransactionException, TransactionAbortedException { System.setSecurityManager(new RMISecurityManager()); String rmiPort = System.getProperty("rmiPort"); if (rmiPort == null) { rmiPort = ""; } else if (!rmiPort.equals("")) { rmiPort = "//:" + rmiPort + "/"; } try { TransactionManagerImpl obj = new TransactionManagerImpl(); Naming.rebind(rmiPort + TransactionManager.RMIName, obj); System.out.println("TM bound"); } catch (Exception e) { System.err.println("TM not bound:" + e); System.exit(1); } enlistList = new HashMap<Integer, Set<ResourceManager>>(); File f = new File(enlistFile); if (f.exists()) { try { ObjectInputStream fin = new ObjectInputStream(new FileInputStream(f)); enlistList = (HashMap<Integer, Set<ResourceManager>>) fin.readObject(); fin.close(); } catch (Exception e) { e.printStackTrace(); } } protocolDB = new HashMap<Integer, Map<String, TwoPC_ST>>(); File f2 = new File(protocolDBFile); if (f2.exists()) { try { ObjectInputStream fin2 = new ObjectInputStream(new FileInputStream(f2)); protocolDB = (HashMap<Integer, Map<String, TwoPC_ST>>) fin2.readObject(); fin2.close(); } catch (Exception e) { e.printStackTrace(); } // recovery for (Integer i : protocolDB.keySet()) { if (i > xidCounter) { xidCounter = i + 1; } Map<String, TwoPC_ST> lastST = protocolDB.get(i); boolean re1 = true; for (TwoPC_ST st : lastST.values()) re1 &= (st == TwoPC_ST.Committed); if (re1) { protocolDB.remove(i); enlistList.remove(i); continue; } System.out.println("TM recovery xid: " + i + " from half commit."); Set<ResourceManager> commitSet = enlistList.get(i); boolean re2 = true; Set<ResourceManager> commitSet2 = new HashSet<ResourceManager>(); for (ResourceManager r : commitSet) { if (r.getStatus(i) == TwoPC_ST.Prepared) { System.out.println("TM recovery " + i + " in " + r.getMyRMIName()); commitSet2.add(r); } else if (r.getStatus(i) == TwoPC_ST.Committed) ; else re2 = false; } System.out.println("here 1"); if (re2) { for (ResourceManager r : commitSet2) { r.commit(i); } protocolDB.remove(i); enlistList.remove(i); } } } }
public boolean commit(int xid) throws RemoteException, TransactionAbortedException, InvalidTransactionException { System.out.println("TM starts to commit " + xid); if (!xidIsValid(xid)) { File f = new File(logFile); if (f.exists()) throw new TransactionAbortedException(xid, ""); else throw new InvalidTransactionException(xid, ""); } if (!enlistList.containsKey(xid)) { throw new TransactionAbortedException(xid, "Nonexist xid for TM."); } Set<ResourceManager> commitSet = enlistList.get(xid); System.out.println(xid + " TM commitSet size: " + commitSet.size()); ResourceManager rm = null; Iterator<ResourceManager> i = commitSet.iterator(); try { while (i.hasNext()) { rm = i.next(); rm.vote(xid, TwoPC_ST.Prepared); rm.getMyRMIName(); System.out.println(rm.getMyRMIName() + " in TM commit " + xid); } } catch (Exception e) { System.out.println("When vote, there is an exception"); enlistList.get(xid).remove(rm); abort(xid); throw new TransactionAbortedException(xid, "invalid rm"); } if (dieTMBeforeCommit) dieNow(); // log "committed" here, and enlistList try { File logF = new File(logFile); FileWriter fw = new FileWriter(logF.getAbsoluteFile(), true); bw = new BufferedWriter(fw); bw.write(Integer.toString(xid)); bw.write(" committed\n"); bw.close(); ObjectOutputStream protocolDBBackup = new ObjectOutputStream(new FileOutputStream(protocolDBFile)); protocolDBBackup.writeObject(protocolDB); protocolDBBackup.close(); ObjectOutputStream enlistFileBackup = new ObjectOutputStream(new FileOutputStream(enlistFile)); enlistFileBackup.writeObject(enlistList); enlistFileBackup.close(); } catch (IOException e) { e.printStackTrace(); } if (dieTMAfterCommit) dieNow(); for (ResourceManager r : commitSet) { r.commit(xid); } protocolDB.remove(xid); enlistList.remove(xid); try { ObjectOutputStream protocolDBBackup = new ObjectOutputStream(new FileOutputStream(protocolDBFile)); protocolDBBackup.writeObject(protocolDB); protocolDBBackup.close(); // enlistFile ObjectOutputStream enlistFileBackup = new ObjectOutputStream(new FileOutputStream(enlistFile)); enlistFileBackup.writeObject(enlistList); enlistFileBackup.close(); } catch (IOException e) { e.printStackTrace(); } return true; }