/** * Service method for backtracking with serialization-based saving of {@link ConstraintNetwork}s. * This method backs up {@link ConstraintNetwork}s before branching. */ private HashMap<ConstraintSolver, byte[]> backupCNs(MultiConstraintSolver conSol) { // Here we want to save the CNs ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; HashMap<ConstraintSolver, byte[]> currentLevel = new HashMap<ConstraintSolver, byte[]>(); try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); for (ConstraintSolver cs : conSol.getConstraintSolvers()) { logger.finest("Backing up CN of " + cs.getClass().getSimpleName()); ConstraintNetwork cn = cs.getConstraintNetwork(); oos.writeObject(cn); byte[] backup = bos.toByteArray(); currentLevel.put(cs, backup); if (cs instanceof MultiConstraintSolver) { // System.out.println("RECURSIVE on " + cs.getClass().getSimpleName()); HashMap<ConstraintSolver, byte[]> lower = backupCNs((MultiConstraintSolver) cs); currentLevel.putAll(lower); } } return currentLevel; } catch (NotSerializableException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
@Test public void testNonSerializable() throws IOException { try { final EventEnablingPredicateFilter<DateDetector> filter = new EventEnablingPredicateFilter<DateDetector>( new DateDetector(AbsoluteDate.J2000_EPOCH), new DummyNonSerializablePredicate()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(filter); Assert.fail("an exception should habe been thrown"); } catch (NotSerializableException nse) { Assert.assertTrue(nse.getMessage().contains("DummyNonSerializablePredicate")); } }
// Object must be Serializable public static byte[] objectToByteArray(Object obj) throws NotSerializableException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(obj); return baos.toByteArray(); } catch (NotSerializableException e) { // this is the only IOException that // shouldn't signal a bizarre error... e.fillInStackTrace(); throw e; } catch (IOException e) { e.printStackTrace(); throw new Error("IOException writing to a byte array!"); } }
/** * Serialize the object o (and any Serializable objects it refers to) and store its serialized * state in File f. */ public static void store(Serializable o, File f) throws IOException { FileOutputStream file = new FileOutputStream(f); ObjectOutputStream out = new ObjectOutputStream(file); try { out.writeObject(o); } catch (java.io.NotSerializableException e) { System.err.println( "Error: Object " + o.getClass() + " is not serializable - run settings cannot be stored."); e.printStackTrace(); } out.flush(); out.close(); file.close(); }
/** * Serialize and save a variable. * * @param field The variable as a field. */ @SuppressWarnings("resource") public synchronized void saveField(Field field) { if (field.getName().equals("updateQueue")) { return; } File tempFile = new File("./Data/server/" + field.getName() + ".tmp"); tempFile.delete(); try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(tempFile)); out.writeObject(field.get(this)); out.close(); new File("./Data/server/" + field.getName() + ".var").delete(); tempFile.renameTo(new File("./Data/server/" + field.getName() + ".var")); } catch (NotSerializableException e) { e.printStackTrace(); return; } catch (IOException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } tempFile.delete(); }
@Override public void execute(CommandExecutor executor, String[] args) { if (args.length == 1) { Player who = executor.getServer().findPlayer(args[0]); if (who != null) { if (executor.getServer().findPlayer(args[0]) == executor) { executor.sendMessage("You can't change your own rank!"); return; } if (who.getGroup().permissionlevel >= executor.getGroup().permissionlevel && (!(executor instanceof Console))) { executor.sendMessage("You can't rank players of the equal or higher rank!"); return; } Group toRank = GroupActions.getPreviousRank(args[0]); if (toRank == null) { executor.sendMessage("No lower ranks exist!"); return; } if (toRank.permissionlevel >= executor.getGroup().permissionlevel && (!(executor instanceof Console))) { executor.sendMessage("You can't rank players to a rank higher than or equal to yours!"); return; } try { if (GroupManagerAPI.demotePlayer(args[0])) { Player pl = executor.getServer().findPlayer(args[0]); executor .getServer() .sendGlobalMessage( pl.getDisplayColor() + pl.username + executor.getServer().defaultColor + " was demoted to " + toRank.color + toRank.name); } else { executor.sendMessage("Failed to set player's rank!"); } } catch (NotSerializableException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { Group ranked = Group.getGroup(args[0]); if (ranked == null) { Group.getDefault().addMember(args[0]); ranked = Group.getDefault(); } if (ranked.permissionlevel >= executor.getGroup().permissionlevel && (!(executor instanceof Console))) { executor.sendMessage("You can't rank players of the equal or higher rank!"); return; } Group toRank = GroupActions.getPreviousRank(args[0]); if (toRank == null) { executor.sendMessage("No lower ranks exist!"); return; } if (toRank.permissionlevel >= executor.getGroup().permissionlevel && (!(executor instanceof Console))) { executor.sendMessage("You can't rank players to a rank higher than or equal to yours!"); return; } try { if (GroupManagerAPI.demotePlayer(args[0])) { executor .getServer() .sendGlobalMessage( "&f(Offline)" + args[0] + executor.getServer().defaultColor + " was demoted to " + toRank.color + toRank.name); } else { executor.sendMessage("Failed to set player's rank!"); } } catch (NotSerializableException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } else { help(executor); return; } }