Package org.wso2.carbon.registry.synchronization

Examples of org.wso2.carbon.registry.synchronization.SynchronizationException


            outputFile = workingDir + File.separator + outputFile;
        }

        try {
            if (!registry.resourceExists(checkOutPath)) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_DUMPING_NO_RESOURCE_OR_NO_PERMISSION,
                        new String[]{"path: " + checkOutPath, "username: " + username});
            }
        } catch (Exception e) {
            throw new SynchronizationException(MessageCode.ERROR_IN_DUMPING_AUTHORIZATION_FAILED, e,
                    new String[]{"path: " + checkOutPath, "username: " + username});
        }

        try {
            // we don't care what is dumping..
            // doing the dump
            ZipOutputStream zos = new
                    ZipOutputStream(new FileOutputStream(outputFile));
            ZipEntry ze = new ZipEntry(outputXml);
            ze.setMethod(ZipEntry.DEFLATED);
            zos.putNextEntry(ze);


            Writer zipWriter = new OutputStreamWriter(zos);

            registry.dump(checkOutPath, zipWriter);

            zos.close();
        } catch (Exception e) {
            throw new SynchronizationException(MessageCode.ERROR_IN_DUMPING, e,
                    new String[]{"path: " + checkOutPath, "username: " + username});
        }

        if (cleanRegistry && registryUrl == null) {
            Utils.cleanEmbeddedRegistry();
View Full Code Here


        // first try getting the path and confirmed it is a collection
        Resource r;
        try {
            r = registry.get(checkOutPath);
        } catch (Exception e) {
            throw new SynchronizationException(
                    MessageCode.ERROR_IN_DUMPING_NO_RESOURCE_OR_NO_PERMISSION,
                    new String[]{"path: " + checkOutPath, "username: " + username});
        }
        if (!(r instanceof Collection)) {
            throw new SynchronizationException(MessageCode.DUMPING_NON_COLLECTION,
                    new String[]{"path: " + checkOutPath, "username: " + username});
        }

        // we are doing the checkout through a temp file. (so assumed enough spaces are there)
        File tempFile = null;
        boolean deleteTempFileFailed = false;
        FileWriter writer = null;
        try {
            try {
                tempFile = File.createTempFile(SynchronizationConstants.DUMP_META_FILE_NAME,
                        SynchronizationConstants.META_FILE_EXTENSION);
                try {
                    writer = new FileWriter(tempFile);
                    // doing the dump
                    registry.dump(checkOutPath, writer);
                } finally {
                    if (writer != null) {
                        writer.close();
                    }
                }
            } catch (RegistryException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_DUMPING_NO_RESOURCE_OR_NO_PERMISSION,
                        new String[]{"path: " + checkOutPath, "username: " + username});
            } catch (IOException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_CREATING_TEMP_FILE_FOR_DUMP,
                        e);
            }
            // now read the xml stream from the file
            XMLStreamReader xmlReader = null;
            Reader reader = null;
            try {
                try {
                    reader = new FileReader(tempFile);
                    xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(reader);
                    checkOutRecursively(xmlReader, workingDir, checkOutPath, callback);
                } finally {
                    try {
                        if (xmlReader != null) {
                            xmlReader.close();
                        }
                    } finally {
                        if (reader != null) {
                            reader.close();
                        }
                    }
                }
            } catch (IOException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_READING_TEMP_FILE_OF_DUMP, e);
            } catch (XMLStreamException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_READING_STREAM_OF_TEMP_FILE_OF_DUMP, e);
            }
        } finally {
            if (tempFile != null) {
                // Our intention here is to delete the temporary file. We are not bothered whether
                // this operation fails.
                deleteTempFileFailed = !tempFile.delete();
            }
        }
        if (deleteTempFileFailed) {
            throw new SynchronizationException(MessageCode.ERROR_IN_CLEANING_UP,
                    new String[]{"file path: " + tempFile.getAbsolutePath()});
        }

        if (cleanRegistry && registryUrl == null) {
            Utils.cleanEmbeddedRegistry();
View Full Code Here

                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;
View Full Code Here

            }
        } else {
            // get the update details form the meta element of the current checkout
            OMElement metaOMElement = Utils.getMetaOMElement(workingDir);
            if (metaOMElement == null) {
                throw new SynchronizationException(MessageCode.CHECKOUT_BEFORE_UPDATE);
            }
            registryUrl = metaOMElement.getAttributeValue(new QName("registryUrl"));
            checkOutPath = metaOMElement.getAttributeValue(new QName("path"));
        }
    }
View Full Code Here

     * @throws SynchronizationException if the operation failed.
     */
    public void execute(Registry registry, UserInputCallback callback) throws
            SynchronizationException {
        if (outputFile != null) {
            throw new SynchronizationException(MessageCode.OUTPUT_FILE_NOT_SUPPORTED);
        }

        // we are doing the update through a temp file. (so assumed enough spaces are there)
        File tempFile = null;
        boolean deleteTempFileFailed = false;
        FileWriter writer = null;
        try {
            try {
                tempFile = File.createTempFile(SynchronizationConstants.DUMP_META_FILE_NAME,
                        SynchronizationConstants.META_FILE_EXTENSION);
                try {
                    writer = new FileWriter(tempFile);
                    // doing the dump
                    registry.dump(checkOutPath, writer);
                } finally {
                    if (writer != null) {
                        writer.close();
                    }
                }
            } catch (RegistryException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_DUMPING_NO_RESOURCE_OR_NO_PERMISSION, e,
                        new String[]{"path: " + checkOutPath, "username: " + username,
                                "registry url: " + registryUrl});
            } catch (IOException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_CREATING_TEMP_FILE_FOR_DUMP);
            }
            // now read the xml stream from the file
            XMLStreamReader xmlReader = null;
            Reader reader = null;
            try {
                try {
                    reader = new FileReader(tempFile);
                    xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(reader);
                    updateRecursively(xmlReader, workingDir, checkOutPath, callback);
                } finally {
                    try {
                        if (xmlReader != null) {
                            xmlReader.close();
                        }
                    } finally {
                        if (reader != null) {
                            reader.close();
                        }
                    }
                }
            } catch (IOException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_READING_TEMP_FILE_OF_DUMP);
            } catch (XMLStreamException e) {
                throw new SynchronizationException(
                        MessageCode.ERROR_IN_READING_STREAM_OF_TEMP_FILE_OF_DUMP);
            }
        } finally {
            if (tempFile != null) {
                // Our intention here is to delete the temporary file. We are not bothered whether
                // this operation fails.
                deleteTempFileFailed = !tempFile.delete();
            }
        }
        if (deleteTempFileFailed) {
            throw new SynchronizationException(MessageCode.ERROR_IN_CLEANING_UP,
                    new String[]{"file path: " + tempFile.getAbsolutePath()});
        }

        if (cleanRegistry && registryUrl == null) {
            Utils.cleanEmbeddedRegistry();
View Full Code Here

        File metaFile = new File(metaFilePath);

        if (file.exists()) {
            isUpdating = true;
            if (isCollection != file.isDirectory()) {
                throw new SynchronizationException(
                        MessageCode.COLLECTION_AND_RESOURCE_SAME_NAME,
                        new String[]{"file name: " + filePath});
            }

            if (metaFile.exists()) {
                // we need to check the last updated times of the current resource
                // and the updating resource

                // get the meta file OMElement
                OMElement metaFileElement = Utils.getOMElementFromMetaFile(metaFilePath);

                OMElement metaFileVersionElement =
                        metaFileElement.getFirstChildWithName(new QName("version"));
                if (metaFileVersionElement == null) {
                    throw new SynchronizationException(MessageCode.UNSUPPORTED_DUMP_FORMAT,
                            new String[]{"missing element: version",
                                    "meta file path: " + metaFilePath});
                }
                OMElement updatingVersionElement = root.getFirstChildWithName(new QName("version"));
                if (updatingVersionElement == null) {
                    throw new SynchronizationException(MessageCode.CHECKOUT_OLD_VERSION,
                            new String[]{"missing element: version", "path: " + path});
                }

                String metaFileVersionStr = metaFileVersionElement.getText();
                String updatingVersionStr = updatingVersionElement.getText();

                if (isCollection) {
                    if (metaFileVersionStr.equals(updatingVersionStr)) {
                        // so there is no server updates for the collection
                        Utils.createMetaFile(metaFilePath, root);
                        collectionIsNotUpdated = true;
                    }
                } else {
                    // here we not just check server side updates, but also check local changes using md5s
                    byte[] currentFileContent = Utils.getBytesFromFile(file);

                    String metaFileMD5 = metaFileElement.getAttributeValue(new QName("md5"));
                    String currentMD5 = Utils.getMD5(currentFileContent);

                    if (metaFileMD5 != null && metaFileMD5.equals(currentMD5)) {
                        // there is no modifications happens to the current file locally,
                        if (metaFileVersionStr.equals(updatingVersionStr)) {
                            // the file in the server is not updated, so just keep the current file locally.
                            // so we are only storing the meta information in the meta file.
                            Utils.createMetaFile(metaFilePath, root);
                            return;
                        } else {
                            // there is a server update to the file, so lets allow it to update the local one
                            // local one is not updated, so it will be overwritten by the server one
                        }
                    } else if (metaFileVersionStr.equals(updatingVersionStr)) {
                        // there is no server side changes, but there are client side changes,
                        // just don't update the content, but let the meta file get updated.
                        Utils.createMetaFile(metaFilePath, root);
                        return;
                    } else {
                        // this is the following scenario
                        // (!metaFileMD5.equals(currentMD5) &&
                        //    !metaFileVersionStr.equals(updatingVersionStr))
                        if (updatingMD5 != null && !updatingMD5.equals(currentMD5)) {
                            isConflicting = true;
                            root.addAttribute("md5", "", null);
                        }
                    }
                }
            } else if (!isCollection) {
                // if there is no meta file exists, that mean there is a conflict
                // a new resource is created both locally and in server
                isConflicting = true;
            }

            if (isConflicting && !isSilentUpdate) {
                // should rename the current file as file.mine
                String mineFileName = filePath + SynchronizationConstants.MINE_FILE_POSTFIX;
                File mineFile = new File(mineFileName);
                Utils.copy(file, mineFile);

                // updating the current file as versionedFileName
                String versionedFileName = filePath + SynchronizationConstants.SERVER_FILE_POSTFIX;
                file = new File(versionedFileName);

                // set the conflicting flag
                root.addAttribute("conflicting", "true", null);
            }
        } else if (isSilentUpdate) {
            // if no files exists locally, the silent update will return
            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;
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

TOP

Related Classes of org.wso2.carbon.registry.synchronization.SynchronizationException

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.