Package org.wso2.carbon.registry.synchronization.message

Examples of org.wso2.carbon.registry.synchronization.message.Message


        if (!isCollection && file.exists()) {
            fileAlreadyExist = true;
            // File already exists, we are asking whether we will skip the file or overwrite it.
            // The default behaviour is to overwrite.
            if (callback != null &&
                    !callback.getConfirmation(new Message(
                            MessageCode.FILE_OVERWRITE_CONFIRMATION,
                            new String[]{filePath}),
                            SynchronizationConstants.OVERWRITE_CONFIRMATION_CONTEXT)) {
                overwrite = false;
            }
        }
        try {
            // Create file if it does not exist
            if (isCollection) {
                boolean ignore = file.mkdir(); // ignores the return value purposely
            } else if (overwrite) {
                boolean ignore = file.createNewFile(); // ignores the return value purposely
            }
        } catch (IOException e) {
            throw new SynchronizationException(MessageCode.FILE_CREATION_FAILED, e,
                    new String[]{
                            "file: " + filePath});
        }

        // we are extracting the content from the meta element.
        Iterator children = root.getChildren();
        while (children.hasNext()) {
            OMElement child = (OMElement) children.next();
            String localName = child.getLocalName();

            // LastModified
            if (localName.equals("lastModified")) {
                OMText text = (OMText) child.getFirstOMChild();
                if (text != null) {
                    long date = Long.parseLong(text.getText());
                    // We are not bothered whether this failed to set the last-modified time. If we
                    // cannot modify the file, we would fail when attempting to write to it anyway.
                    boolean ignore = file.setLastModified(date);
                }
            }
            // get content
            else if (localName.equals("content")) {
                OMText text = (OMText) child.getFirstOMChild();
                // we keep content as base64 encoded
                if (text != null) {
                    contentBytes = Base64.decode(text.getText());
                }
                String md5 = Utils.getMD5(contentBytes);
                root.addAttribute("md5", md5, null);
                child.detach();
            }
        }

        if (!isCollection && overwrite) {
            try {
                FileOutputStream fileStream = null;
                try {
                    fileStream = new FileOutputStream(file);
                    fileStream.write(contentBytes);
                    fileStream.flush();
                } finally {
                    if (fileStream != null) {
                        fileStream.close();
                    }
                }
            } catch (IOException e) {
                throw new SynchronizationException(MessageCode.PROBLEM_IN_CREATING_CONTENT,
                        e,
                        new String[]{"file: " + filePath});
            }
        }

        String parentDirName = file.getParent();
        // creating the meta directory
        String metaDirectoryName;
        if (isCollection) {
            metaDirectoryName = filePath + File.separator + SynchronizationConstants.META_DIRECTORY;
        } else {
            metaDirectoryName =
                    parentDirName + File.separator + SynchronizationConstants.META_DIRECTORY;
        }
        File metaDirectory = new File(metaDirectoryName);
        if (!metaDirectory.exists() && !metaDirectory.mkdir()) {
            throw new SynchronizationException(MessageCode.ERROR_CREATING_META_FILE,
                    new String[]{"file: " + metaDirectoryName});
        }

        // creating the meta file
        String metaFilePath;
        if (isCollection) {
            metaFilePath = filePath + File.separator + SynchronizationConstants.META_DIRECTORY +
                    File.separator +
                    SynchronizationConstants.META_FILE_PREFIX +
                    SynchronizationConstants.META_FILE_EXTENSION;
        } else {
            metaFilePath =
                    parentDirName + File.separator + SynchronizationConstants.META_DIRECTORY +
                            File.separator + SynchronizationConstants.META_FILE_PREFIX +
                            Utils.encodeResourceName(name) +
                            SynchronizationConstants.META_FILE_EXTENSION;
        }
        Utils.createMetaFile(metaFilePath, root);

        // printing out the information of the file
        if (!fileAlreadyExist) {
            if (callback != null) {
                callback.displayMessage(new Message(MessageCode.ADDED, new String[]{filePath}));
            }
            addedCount++;
        } else {
            if (overwrite) {
                if (callback != null) {
                    callback.displayMessage(
                            new Message(MessageCode.OVERWRITTEN, new String[]{filePath}));
                }
                overwrittenCount++;
            } else {
                if (callback != null) {
                    callback.displayMessage(
                            new Message(MessageCode.NON_OVERWRITTEN, new String[]{filePath}));
                }
                nonOverwrittenCount++;
            }
        }
View Full Code Here


            return;
        } else if (metaFile.exists()) {
            // now the meta file is there but the direct file is deleted, so we will
            // ask whether he want to get the up. Default behaviour is not to take the update from
            // the server. This is because the working copy is up-to-date.
            if (callback == null || callback.getConfirmation(new Message(
                    MessageCode.KEEP_DELETED_FILE,
                        new String[]{filePath}),
                        SynchronizationConstants.DELETE_CONFIRMATION_CONTEXT)) {
                return;
            }
        }
        if (!isUpdating) {
            try {
                // Create file if it does not exist
                if (isCollection) {
                    boolean ignore = file.mkdir(); // ignores the return value purposely
                } else {
                    boolean ignore = file.createNewFile(); // ignores the return value purposely
                }
            } catch (IOException e) {
                throw new SynchronizationException(MessageCode.FILE_CREATION_FAILED, e,
                        new String[]{"file name: " + filePath});
            }
        }

        if (!isCollection) {
            try {
                boolean writeToFile = true;

                if (file.exists()) {
                    byte[] currentContentBytes = Utils.getBytesFromFile(file);
                    if (currentContentBytes != null && contentBytes != null) {
                        String currentContentMd5 = Utils.getMD5(currentContentBytes);
                        String writingContentMd5 = Utils.getMD5(contentBytes);
                        if (writingContentMd5 != null &&
                                writingContentMd5.equals(currentContentMd5)) {
                            writeToFile = false;
                        }
                    }
                }
                if (writeToFile) {
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    fileOutputStream.write(contentBytes);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }

            } catch (IOException e) {
                throw new SynchronizationException(MessageCode.PROBLEM_IN_CREATING_CONTENT,
                        e,
                        new String[]{"file name: " + filePath});
            }
        } else {
            // creating the meta directory
            String metaDirectoryName =
                    filePath + File.separator + SynchronizationConstants.META_DIRECTORY;
            File metaDirectory = new File(metaDirectoryName);
            if (!metaDirectory.exists() && !metaDirectory.mkdir()) {
                throw new SynchronizationException(MessageCode.ERROR_CREATING_META_FILE,
                        new String[]{"file: " + metaDirectoryName});
            }
        }

        boolean iterateChildren = true;

        if (!xmlReader.hasNext() || !(xmlReader.isStartElement() &&
                xmlReader.getLocalName().equals("children"))) {
            // finished the recursion
            // consuming the stream until the resource end element found
            while (xmlReader.hasNext() && !(xmlReader.isEndElement() &&
                    xmlReader.getLocalName().equals("resource"))) {
                xmlReader.next();
            }
            iterateChildren = false;
        }
        if (iterateChildren) {
            do {
                xmlReader.next();
                if (xmlReader.isEndElement() && xmlReader.getLocalName().equals("children")) {
                    // this means empty children, just quit from here
                    // before that we have to set the cursor to the end of the current resource
                    if (xmlReader.hasNext()) {
                        do {
                            xmlReader.next();
                        } while (xmlReader.hasNext() && !(xmlReader.isEndElement() &&
                                xmlReader.getLocalName().equals("resource")));
                    }
                    iterateChildren = false;
                    break;
                }
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }

        Map<String, Boolean> childNames = new HashMap<String, Boolean>();

        if (iterateChildren) {
            while (xmlReader.hasNext() && xmlReader.isStartElement() &&
                    xmlReader.getLocalName().equals("resource")) {
                // prepare the children absolute path

                String childName = xmlReader.getAttributeValue(null, "name");
                String fileResourceName = Utils.encodeResourceName(childName);
                String childFilePath = filePath + File.separator + fileResourceName;
                String childPath = (path.equals("/") ? "" : path) + "/" + childName;

                updateRecursively(xmlReader, childFilePath, childPath, callback);
                childNames.put(fileResourceName, true);


                while ((!xmlReader.isStartElement() && xmlReader.hasNext()) &&
                        !(xmlReader.isEndElement() &&
                                xmlReader.getLocalName().equals("children"))) {
                    xmlReader.next();
                }
                if (xmlReader.isEndElement() && xmlReader.getLocalName().equals("children")) {
                    // we are in the end of the children tag.
                    break;
                }
            }
            // consuming the stream until the resource end element found
            while (xmlReader.hasNext() && !(xmlReader.isEndElement() &&
                    xmlReader.getLocalName().equals("resource"))) {
                xmlReader.next();
            }

            // now we are checking which files have been deleted at the server end.
            String[] childFileNames = file.list();
            if (childFileNames != null) {
                for (String childFileName : childFileNames) {
                    if (childFileName.equals(SynchronizationConstants.META_DIRECTORY)) {
                        continue;
                    }
                    if (childNames.get(childFileName) != null && childNames.get(childFileName)) {
                        // this files stays on the server as well, so nothing to worry
                        continue;
                    }
                    // hm, we have a situation that stuff exist local, but not at the server
                    // first need to check whether they are newly added.
                    // we can do that by checking the existence of meta directory
                    String childFilePath = file + File.separator + childFileName;
                    File childFile = new File(file, childFileName);
                    boolean shouldDelete = false;
                    File childMetaFile;
                    if (childFile.isDirectory()) {
                        // the meta directory should exist in .meta
                        String metaDirName =
                                filePath + File.separator + childFileName + File.separator +
                                        SynchronizationConstants.META_DIRECTORY;
                        childMetaFile = new File(metaDirName);
                        if (childMetaFile.exists()) {
                            // looks like it's bean earlier checkout from registry, mean it is now deleted
                            shouldDelete = true;
                        }
                    } else {
                        String metaFileName =
                                filePath + File.separator +
                                        SynchronizationConstants.META_DIRECTORY + File.separator +
                                        SynchronizationConstants.META_FILE_PREFIX + childFileName +
                                        SynchronizationConstants.META_FILE_EXTENSION;
                        childMetaFile = new File(metaFileName);
                        if (childMetaFile.exists()) {
                            // looks like it's bean earlier checkout from registry, mean it is now deleted
                            shouldDelete = true;
                        }
                    }
                    if (shouldDelete && !isSilentUpdate) {
                        boolean isDeleted = Utils.confirmDelete(childFile, childMetaFile, callback);
                        if (isDeleted) {
                            if (callback != null && !isSilentUpdate) {
                                callback.displayMessage(new Message(MessageCode.DELETED,
                                        new String[]{refinedPathToPrint(childFilePath)}));
                            }
                            deletedCount++;
                        } else {
                            if (callback != null && !isSilentUpdate) {
                                callback.displayMessage(new Message(MessageCode.NOT_DELETED,
                                        new String[]{refinedPathToPrint(childFilePath)}));
                            }
                            notDeletedCount++;
                        }
                    }
                }
            }
        }

        if (file.isDirectory() && collectionIsNotUpdated) {
            return;
        }

        // creating the meta file
        String metaFileName;
        if (isCollection) {
            metaFileName = filePath + File.separator + SynchronizationConstants.META_DIRECTORY +
                    File.separator +
                    SynchronizationConstants.META_FILE_PREFIX +
                    SynchronizationConstants.META_FILE_EXTENSION;
        } else {
            String parentDirName = file.getParent();
            metaFileName =
                    parentDirName + File.separator + SynchronizationConstants.META_DIRECTORY +
                            File.separator + SynchronizationConstants.META_FILE_PREFIX +
                            Utils.encodeResourceName(name) +
                            SynchronizationConstants.META_FILE_EXTENSION;
        }
        Utils.createMetaFile(metaFileName, root);

        // printing out the information of the file
        if (isConflicting) {
            if (callback != null && !isSilentUpdate) {
                callback.displayMessage(new Message(MessageCode.CONFLICTED,
                        new String[]{refinedPathToPrint(filePath)}));
            }
            conflictedCount++;
        } else if (isUpdating) {
            if (callback != null && !isSilentUpdate) {
                callback.displayMessage(new Message(MessageCode.UPDATED,
                        new String[]{refinedPathToPrint(filePath)}));
            }
            updatedCount++;
        } else {
            if (callback != null && !isSilentUpdate) {
                callback.displayMessage(new Message(MessageCode.ADDED,
                        new String[]{refinedPathToPrint(filePath)}));
            }
            addedCount++;
        }
    }
View Full Code Here

        if (callback == null) {
            // The default behaviour is to delete.
            inputCode = true;
        } else if (file.isDirectory()) {
            inputCode = callback.getConfirmation(new Message(
                    MessageCode.DIRECTORY_DELETE_CONFIRMATION,
                    new String[]{"file path: " + filePath}),
                    SynchronizationConstants.DELETE_CONFIRMATION_CONTEXT);
        } else {
            inputCode = callback.getConfirmation(new Message(
                    MessageCode.FILE_DELETE_CONFIRMATION,
                    new String[]{"file path: " + filePath}),
                    SynchronizationConstants.DELETE_CONFIRMATION_CONTEXT);
        }
View Full Code Here

     * @param cause      the cause of this exception.
     * @param parameters the parameters related to this exception.
     */
    public SynchronizationException(MessageCode code, Throwable cause, String[] parameters) {
        super(DEFAULT_SYNCHRONIZATION_OPERATION_FAILED_MESSAGE, cause);
        this.message = new Message(code, parameters != null ? parameters : new String[0]);
    }
View Full Code Here

     *
     * @param code the message code of the error.
     */
    public SynchronizationException(MessageCode code) {
        super(DEFAULT_SYNCHRONIZATION_OPERATION_FAILED_MESSAGE);
        this.message = new Message(code, new String[0]);
    }
View Full Code Here

        }

        public void run() {
            try {
                MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                Event<MessageContext> event = new Event(msgCtx);
                subscriptions = subscriptionManager.getMatchingSubscriptions(event);
            } catch (EventException e) {
                handleException("Matching subscriptions fetching error", e);
            }
View Full Code Here

     * @throws EventException event
     */
    private void processGetStatusRequest(MessageContext mc,
                                         ResponseMessageBuilder messageBuilder)
            throws AxisFault, EventException {
        Subscription subscription =
                SubscriptionMessageBuilder.createGetStatusMessage(mc);
        if (log.isDebugEnabled()) {
            log.debug("GetStatus request recived for SynapseSubscription ID : " +
                    subscription.getId());
        }
        subscription = subscriptionManager.getSubscription(subscription.getId());
        if (subscription != null) {
            if (log.isDebugEnabled()) {
                log.debug("Sending GetStatus responce for SynapseSubscription ID : " +
                        subscription.getId());
            }
            //send the responce
            SOAPEnvelope soapEnvelope = messageBuilder.genGetStatusResponse(subscription);
            dispatchResponse(soapEnvelope, EventingConstants.WSE_GET_STATUS_RESPONSE,
                    mc, false);
View Full Code Here

            // Adding static subscriptions
            List<Subscription> staticSubscriptionList =
                    eventSource.getSubscriptionManager().getStaticSubscriptions();
            for (Iterator<Subscription> iterator = staticSubscriptionList.iterator();
                 iterator.hasNext();) {
                Subscription staticSubscription = iterator.next();
                OMElement staticSubElem =
                        fac.createOMElement("subscription", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                staticSubElem.addAttribute(
                        fac.createOMAttribute("id", nullNS, staticSubscription.getId()));
                OMElement filterElem =
                        fac.createOMElement("filter", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                filterElem.addAttribute(fac.createOMAttribute("source", nullNS,
                        (String) staticSubscription.getFilterValue()));
                filterElem.addAttribute(fac.createOMAttribute("dialect", nullNS,
                        (String) staticSubscription.getFilterDialect()));
                staticSubElem.addChild(filterElem);
                OMElement endpointElem =
                        fac.createOMElement("endpoint", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                OMElement addressElem =
                        fac.createOMElement("address", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                addressElem.addAttribute(
                        fac.createOMAttribute("uri", nullNS, staticSubscription.getEndpointUrl()));
                endpointElem.addChild(addressElem);
                staticSubElem.addChild(endpointElem);
                if (staticSubscription.getExpires() != null) {
                    OMElement expiresElem =
                            fac.createOMElement("expires", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                    fac.createOMText(expiresElem,
                            ConverterUtil.convertToString(staticSubscription.getExpires()));
                    staticSubElem.addChild(expiresElem);
                }
                evenSourceElem.addChild(staticSubElem);
            }
View Full Code Here


    public SynapseSubscription() {
        this.setId(UIDGenerator.generateURNString());
        this.setDeliveryMode(EventingConstants.WSE_DEFAULT_DELIVERY_MODE);
        SubscriptionData subscriptionData = new SubscriptionData();
        subscriptionData.setProperty(SynapseEventingConstants.STATIC_ENTRY, "false");
        this.setSubscriptionData(subscriptionData);
    }
View Full Code Here

                    .getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "class"));
            if (clazz != null) {
                String className = clazz.getAttributeValue();
                try {
                    Class subscriptionManagerClass = Class.forName(className);
                    SubscriptionManager manager =
                            (SubscriptionManager) subscriptionManagerClass.newInstance();
                    Iterator itr = subscriptionManagerElem.getChildrenWithName(PROPERTIES_QNAME);
                    while (itr.hasNext()) {
                        OMElement propElem = (OMElement) itr.next();
                        String propName =
                                propElem.getAttribute(new QName("name")).getAttributeValue();
                        String propValue =
                                propElem.getAttribute(new QName("value")).getAttributeValue();
                        if (propName != null && !"".equals(propName.trim()) &&
                                propValue != null && !"".equals(propValue.trim())) {

                            propName = propName.trim();
                            propValue = propValue.trim();

                            PasswordManager passwordManager =
                                    PasswordManager.getInstance();
                            String key = eventSource.getName() + "." + propName;

                            if (passwordManager.isInitialized()
                                    && passwordManager.isTokenProtected(key)) {
                                eventSource.putConfigurationProperty(propName, propValue);
                                propValue = passwordManager.resolve(propValue);
                            }

                            manager.addProperty(propName, propValue);
                        }
                    }
                    eventSource.setSubscriptionManager(manager);
                    eventSource.getSubscriptionManager()
                            .init(); // Initialise before doing further processing, required for static subscriptions
View Full Code Here

TOP

Related Classes of org.wso2.carbon.registry.synchronization.message.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.