public class RemoveUserPDBPanel extends PDBPanel { Logger log = Logger.getLogger(RemoveUserPDBPanel.class); private ManagementPanel managementPanel; private Node userToRemove; public RemoveUserPDBPanel( ManagementPanel managementPanel, Schema schema, Node parent, Node userToRemove) { this.managementPanel = managementPanel; this.userToRemove = userToRemove; build(); } protected void build() { add(new PDBLabel("Removing user ")); add(new PDBButton("Ok", new RemoveUserAction())); } private class RemoveUserAction implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { try { // the userName is the arcName String userName = userToRemove.getAttributes().get(ATTRIBUTES_USER_NAME).getStringData(); managementPanel.getStorage().deleteUser(userName); RemoveUserPDBPanel.this.shutDown(); } catch (AbstractPlanckDBException e) { log.error("Fail ro remove user.", e); } } } }
@ShortSingleTone public class Serializator { private static Logger log = Logger.getLogger(Serializator.class); public byte[] serialize(Object data) throws SharedMemorySerializationException { try { if (data == null) { return new byte[0]; } int type = getType(data); switch (type) { case JAVA_OBJECT: { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(data); return byteArrayOutputStream.toByteArray(); } case STRING: { return ((String) data).getBytes(DBProperties.getCharset()); } case INTEGER: { byte[] array = new byte[SIZE_OF_INT]; SerializationUtils.intByteArrayFiller((Integer) data, array, 0); return array; } default: throw new SharedMemorySerializationException( CommandStatus.unExpectedException, "Fail to serialize object : " + data); } } catch (IOException e) { throw new SharedMemorySerializationException( CommandStatus.unExpectedException, "Fail to serialize object : " + data); } } public Object deSerialize(byte[] entityData, Integer type) throws SharedMemorySerializationException { if (entityData == null || type == null || entityData.length == 0) { return null; } switch (type) { case JAVA_OBJECT: { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(entityData); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); return objectInputStream.readObject(); } catch (Exception e) { throw new SharedMemorySerializationException( CommandStatus.unExpectedException, "Fail to deSerialize object "); } } case STRING: { try { return new String(entityData, DBProperties.getCharset()); } catch (Exception e) { throw new SharedMemorySerializationException( CommandStatus.unExpectedException, "Fail to deSerialize object "); } } case INTEGER: { try { return SerializationUtils.byteArrayToInt(entityData, 0); } catch (Exception e) { throw new SharedMemorySerializationException( CommandStatus.unExpectedException, "Fail to deSerialize object "); } } default: throw new SharedMemorySerializationException( CommandStatus.unExpectedException, "Fail to deSerialize object "); } } public int getType(Object data) { if (data instanceof String) { return STRING; } if (data instanceof Integer) { return INTEGER; } return JAVA_OBJECT; } }
/** * VersionCore :Like its name, is the schema's core and its role to manage the "data changed" * events,conducted by both the server and the client */ @ShortSingleTone public class VersionCore extends Thread implements Core { private static Logger log = Logger.getLogger(VersionCore.class); private @Inject DBProperties dbProperties; private @Inject CoreManager coreManager; private @Inject CommandExecutor commandExecutor; private @Inject CommandQueue commandQueue; private @Inject DistributionManager distributionManager; private @Inject SessionMetaData sessionMetaData; private @Inject Registry registry; private @Inject SchemaAdapter schema; private @Inject AtomicContainer atomicContainer; private @Inject CommandBuilder commandBuilder; private boolean active; public VersionCore() { active = true; } /** * The method does the following: * * <p>1.in case of none atomic command * * <p>- execute the command using the command executor * * <p>- notify all waiting thread about the command execution * * <p>2.in case of atomic command * * <p>- execute the command using the command executor * * <p>- notify all waiting thread about the command execution only * * <p>if the current command is the last element in the atomic command * * @param modelCommand * @throws PlanckDBException */ @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") public void doJobs(final Command modelCommand) throws PlanckDBException { boolean consume = true; if (modelCommand.getTransaction() == TRUE && modelCommand.getSessionId() == sessionMetaData.getSessionId()) { consume = false; } if (consume) { commandExecutor.consume(modelCommand); } if (atomicContainer.isPartOfAtomicCommand(modelCommand)) { Command rootCommand = atomicContainer.update(modelCommand); if (rootCommand != null) { synchronized (rootCommand) { rootCommand.notifyAll(); } } } else { synchronized (modelCommand) { modelCommand.notifyAll(); } } // log.debug("message done version : "+modelCommand.getVersion()+" id // "+modelCommand.getEntityId()); } /** * this is the heart of the storage, * * <p>It control all the in coming commands. * * <p>The method does the following * * <p>1. validate the command * * <p>2. update the version number and the conflict number (if needed) * * <p>3. push the command to the command queue * * <p> */ public void consume(Command command) throws PlanckDBException { // update schema and coreManagerKey in command in case that the schema or coreManagerKey fields // in the command are null if (command.getSchemaId() < 0) { command.setSchemaId(getSessionMetaData().getSchemaId()); } if (command.getCoreManagerKey() == null) { command.setCoreManagerKey(getCoreManager().getKey()); } // you do not have to handle your messages which NetworkProtocolType is multicast // because you have already handle them final NetworkProtocolType type = command.getNetworkProtocolType(); if (type != null && type.isCast() && command.getCoreManagerKey().equals(getCoreManager().getKey()) && sessionMetaData.getSessionId() == command.getSessionId()) { return; } // TODO validate message if (command.isModeCommand() || command.isAtomicModelCommand()) { // model change commands // set version number or distribute if (command.getVersion() < 0) { distributionManager.produceTcp(command); int version = command.getVersion(); // return if command is lock or something was wrong if (version < 0 || command.isNotSucceed()) { return; } } if (command.isAtomicModelCommand()) { List<Command> commands = command.getCommands(); atomicContainer.register(command, commands); for (Command newCommand : commands) { commandQueue.pushCommand(newCommand); } } else { commandQueue.pushCommand(command); } } else { Integer commandType = command.getCommandType(); if (commandType == READ_LOCK_COMMAND) { if (!command.getCoreManagerKey().equals(coreManager.getKey())) { List<Command> commands = command.getCommands(); boolean lock = command.isLocked(); for (Command newCommand : commands) { int entityId = newCommand.getEntityId(); int ownerId = newCommand.getOwnerId(); if (lock) { registry.lockEntity(entityId, true, ownerId); } else { registry.lockEntity(entityId, false, NON_ENTITY_OWNER); } } } else { distributionManager.produceTcp(command); } } } } /** * This method runs in the background * * <p>It waits for new command to be pushed into it, * * <p>and then pop the commands by their version number * * <p>in order to execute them */ @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") public void run() { Command modelCommand = null; while (active) { try { modelCommand = commandQueue.popCommand(); if (modelCommand != null) { doJobs(modelCommand); } } catch (Exception e) { if (active) { log.fatal("An exception occur in the core thread : Expected lost of data ", e); modelCommand.push(COMMAND_STATUS, INTEGER, CommandStatus.unExpectedException.getId()); synchronized (modelCommand) { modelCommand.notify(); } } } } } /** * disconnect from server * * <p>kills all thread related to the schema */ public void close() { active = false; try { interrupt(); } catch (Exception e) { // do nothing interrupting sockets } commandQueue.close(); } public CommandQueue getCommandQueue() { return commandQueue; } public SessionMetaData getSessionMetaData() { return sessionMetaData; } public Registry getRegistry() { return registry; } public Schema getSchema() { return schema; } public void removeModelChangedListener(NodeModelChangedListener nodeModelChangedListener) { commandExecutor.removeModelChangedListener(nodeModelChangedListener); } public CoreManager getCoreManager() { return coreManager; } public DistributionManager getDistributionManager() { return distributionManager; } public void addModelChangedListener(NodeModelChangedListener nodeModelChangedListener) { commandExecutor.addModelChangedListener(nodeModelChangedListener); } }