Package org.jdesktop.wonderland.modules.contentrepo.common

Examples of org.jdesktop.wonderland.modules.contentrepo.common.ContentResource


       
        // Delete the existing avatar locally if there is one. We need to do
        // this before touching the 'avatar' object because they may be one
        // in the same.
        if (existingAvatar != null) {
            ContentResource resource = existingAvatar.getResource();
            ContentCollection parent = resource.getParent();
            try {
                parent.removeChild(resource.getName());
            } catch (ContentRepositoryException excp) {
                logger.log(Level.WARNING, "Unable to remove local avatar " +
                        resource.getPath(), excp);
            }
        }

        // For each server we are connected to, delete the old file from the
        // server
        synchronized (avatarConfigServers) {
            logger.info("Attempting to delete avatar to server, number=" +
                avatarConfigServers.size());

            for (ServerSyncThread t : avatarConfigServers.values()) {
                // If there is an old version, we ask the server to delete the
                // file. We wait for its completion. We need to do this first
                // to get rid of the old avatar before we upload the new one.
                if (existingAvatar != null) {
                    try {
                        logger.info("Schedule delete of existing avatar " +
                                "named " + avatarName + " from server.");
                        t.scheduleDelete(existingAvatar, true);
                    }
                    catch (InterruptedException excp) {
                        logger.log(Level.WARNING, "Attempt to delete the" +
                                " avatar " + avatar.getName() + " from " +
                                "the server " + t.toString() + " was " +
                                "interrupted.", excp);
                    }
                }
            }
        }

        if (existingAvatar != null) {
            logger.info("Already have an avatar named " + avatarName +
                    " with version " + existingAvatar.getVersion());

            // If we already have an avatar, check it's version number,
            // increment it and assign the new version number to this file.
            int version = existingAvatar.getVersion();
            avatar.setVersion(version);
            avatar.incrementVersion();
        }

        // Generate the avatar character from the avatar configuration. We will
        // use this to write out its parameters
        WonderlandCharacterParams params = avatar.getAvatarParams(false);
        WlAvatarCharacter character = ImiAvatar.getAvatarCharacter(params);

        // Create a file to hold the avatar configuration locally if it does
        // not yet exist.
        String fileName = avatar.getFilename();
        ContentResource file = (ContentResource) imiCollection.createChild(fileName, Type.RESOURCE);
        if (file == null) {
            file = (ContentResource) imiCollection.createChild(fileName, Type.RESOURCE);
        }

        logger.info("Writing avatar to resource " + file.getPath());

        // Write out the avatar configuration to a byte avatar.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        character.saveConfiguration(out);
        out.close();

        // Then write out the XML to the local repository. Update the avatar
        // to point to this local avatar resource
        file.put(out.toByteArray());
        avatar.setResource(file);

        // Update the map of local avatars with the new avatar. We always do
        // this no matter whether the avatar is new or whether we are updating
        // an existing avatar.
View Full Code Here


            // Fetch the name of the avatar and see if an entry exists on the
            // server. If so, then remove it from the server and the map.
            String avatarName = avatar.getName();
            ImiServerAvatar serverAvatar = serverAvatars.get(avatarName);
            if (serverAvatar != null) {
                ContentResource resource = serverAvatar.resource;
                ContentCollection parent = resource.getParent();
                try {
                    parent.removeChild(resource.getName());
                } catch (ContentRepositoryException excp) {
                    logger.log(Level.WARNING, "Unable to delete avatar from" +
                            " server " + avatar, excp);
                }
                serverAvatars.remove(avatarName);
View Full Code Here

            // the avatars on the server.
            try {
                List<ContentNode> avatarList = serverCollection.getChildren();
                for (ContentNode node : avatarList) {
                    if (node instanceof ContentResource) {
                        ContentResource resource = (ContentResource)node;
                        ImiServerAvatar serverAvatar = new ImiServerAvatar(resource);
                        String avatarName = serverAvatar.avatarName;

                        logger.info("Looking at server avatar named " +
                                avatarName + " with version " + serverAvatar.version);

                        // Check to see if the server avatar already exists in
                        // the known list of server avatars.
                        ImiServerAvatar previous = serverAvatars.put(avatarName, serverAvatar);
                        if (previous != null && previous.version > serverAvatar.version) {

                            logger.info("Found a previous version named " +
                                    avatarName + " with version " + previous.version);

                            // If we somehow find a more recent avatar in our
                            // list, then we remove the one we just found on
                            // the server. (The most recent one will be added
                            // back to the server later).
                            serverAvatars.put(previous.avatarName, previous);
                            String fileName = serverAvatar.getFilename();
                            serverCollection.removeChild(fileName);
                        }
                    }
                }

                // Make a copy of the map of server avatars. This will serve
                // as a list of all avatars that need to be downloaded from
                // the server.
                Map<String, ImiServerAvatar> tmpServerAvatars = new HashMap(serverAvatars);

                // Loop through all of the avatars we know about locally. See
                // if one by the same name exists on the server. If so, and
                // the server version needs to be updated, then do so. If the
                // local copy needs to be updated then do so.
                synchronized (localAvatars) {

                    logger.info("Taking a look at all of our local avatars");

                    for (ImiAvatar avatar : localAvatars.values()) {
                        String avatarName = avatar.getName();
                        ImiServerAvatar serverVersion = tmpServerAvatars.get(avatarName);

                        logger.info("Looking at local avatar named " +
                                avatarName + " server version " + serverVersion);

                        // If the local avatar is not on the server, or if the
                        // version of the server is older than locally, then
                        // mark this avatar for upload.
                        if (serverVersion == null ||
                                serverVersion.version < avatar.getVersion()) {

                            logger.info("Server version for avatar named " +
                                    avatarName + " does not exist or is older" +
                                    " than version " + avatar.getVersion());

                            uploadList.add(avatar);
                            tmpServerAvatars.remove(avatarName);
                        }
                        else if (serverVersion.version > avatar.getVersion()) {
                            logger.info("Server version for avatar named " +
                                    avatarName + " is more recent than local " +
                                    "with server version " + serverVersion.version +
                                    " and local version " + avatar.getVersion());

                            // Otherwise, if the server has a more recent copy
                            // of the avatar, then mark this avatar for
                            // download.
                            downloadList.add(serverVersion);
                            tmpServerAvatars.remove(avatarName);
                        }
                        else if (serverVersion.version == avatar.getVersion()) {
                            logger.info("Server version for avatar named " +
                                    avatarName + " is same as local version " +
                                    avatar.getVersion());
                           
                            // If the two versions are the same, we just want
                            // to do nothing with it.
                            // XXX Why do we need to re-add it to the server
                            // avatar list? It should already be there.
                            tmpServerAvatars.remove(avatarName);
                            serverAvatars.put(avatarName, serverVersion);
                        }
                    }
                }

                // Avatars left in the serverAvatars map are only on the server,
                // and not on the client (so the previous code block did not
                // see them), so add them to the download list.
                for (ImiServerAvatar serverAvatar : tmpServerAvatars.values()) {
                    logger.info("Adding Server avatar to download list " +
                            serverAvatar.avatarName + " version " +
                            serverAvatar.version);

                    downloadList.add(serverAvatar);
                }


                // For all of the avatar configuration files that we wish to
                // upload, do so synchronously.
                logger.info("Doing upload of local avatars, number to upload " +
                        uploadList.size());

                for (ImiAvatar avatar : uploadList) {
                    logger.info("Uploading Local avatar to server " +
                            avatar.getName() + " version " + avatar.getVersion());

                    uploadFileImpl(avatar);
                }

                // Keep a list around of all of the avatars we have just
                // downloaded, to be added to the set of local avatars later.
                List<ImiAvatar> newAvatarList = new ArrayList();

                // For all of the avatar configuration files that we wish to
                // download, do so synchronously.
                logger.info("Doing download of local avatars to the server," +
                        " number to download " + downloadList.size());

                for (ImiServerAvatar serverAvatar : downloadList) {
                    try {
                        logger.info("Downloading server avatar named " +
                                serverAvatar.avatarName + " to file name " +
                                serverAvatar.resource.getName());

                        // Create an entry for the configuration file locally.
                        // Write the contents of the server version to this file.
                        String fileName = serverAvatar.resource.getName();
                        ContentResource localFile = (ContentResource) imiCollection.createChild(fileName, Type.RESOURCE);
                        InputStream is = serverAvatar.resource.getURL().openStream();
                        localFile.put(new BufferedInputStream(is));

                        // Create a new entry to put on the local list. These
                        // will be added below.
                        ImiAvatar newAvatar = new ImiAvatar(localFile);
                        newAvatarList.add(newAvatar);

                        logger.info("Local avatar created in resource " +
                                localFile.getPath());

                    } catch (IOException excp) {
                        logger.log(Level.WARNING, "Error downloading server " +
                                "avater named " + serverAvatar.avatarName, excp);
                    }
View Full Code Here

         * @param avatar The avatar to upload to the server
         */
        private void uploadFileImpl(ImiAvatar avatar) {
            // Fetch the avatar we wish to upload and the resource that corresponds
            // to the local configuration file.
            ContentResource resource = avatar.getResource();
            String fileName = resource.getName();

            try {
                // Create a resource on the server with the same as the local resource.
                // Assume the file does not yet exist on the server. Then go ahead
                // and upload the local file to the server.
                ContentResource file = (ContentResource) serverCollection.createChild(fileName, Type.RESOURCE);
                InputStream is = resource.getURL().openStream();
                file.put(new BufferedInputStream(is));

                // Add an entry to the map of avatars on the server.
                ImiServerAvatar serverAvatar = new ImiServerAvatar(file);
                serverAvatars.put(serverAvatar.avatarName, serverAvatar);
            } catch (java.lang.Exception excp) {
View Full Code Here

        // the X11 App being deleted.
        String path = request.getParameter("path");
        String appName = request.getParameter("appName");

        // Find the file in the WebDav repository and delete it
        ContentResource resource = getXAppResource(xAppsCollection, path);
        if (resource == null) {
            error(request, response, "Path " + request.getPathInfo() +
                    " not found.");
            return;
        }
        xAppsCollection.removeChild(resource.getName());

        // Tell the config connection that we have removed an X11 App, if the
        // config connection exists (it should)
        Object obj = getServletContext().getAttribute(XAPPS_CONN_ATTR);
        if (obj != null) {
View Full Code Here

            return;
        }
        appNode = xAppsCollection.createChild(nodeName, Type.RESOURCE);

        // Write the XAppRegistryItem object as an XML stream to this new file
        ContentResource resource = (ContentResource)appNode;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Writer w = new OutputStreamWriter(os);
        XAppRegistryItem item = new XAppRegistryItem(appName, command);
        item.encode(w);
        byte b[] = os.toByteArray();
        resource.put(b);

        // Tell the config connection that we have added a new X11 App, if
        // the config connection exists (it should)
        Object obj = getServletContext().getAttribute(XAPPS_CONN_ATTR);
        if (obj != null) {
View Full Code Here

        // be displayed by the jsp.
        Collection<X11AppEntry> entries = new ArrayList();
        for (ContentNode child : c.getChildren()) {
            if (child instanceof ContentResource) {
                // Find out the information about the content resource item
                ContentResource resource = (ContentResource)child;
                String path = resource.getPath();

                // Use JAXB to parse the item
                Reader r = new InputStreamReader(resource.getInputStream());
                XAppRegistryItem item = XAppRegistryItem.decode(r);

                String appName = item.getAppName();
                String command = item.getCommand();
               
View Full Code Here

        // the information to a collection of X11AppEntry objects. This will
        // be displayed by the jsp.
        for (ContentNode child : c.getChildren()) {
            if (child instanceof ContentResource) {
                // Find out the information about the content resource item
                ContentResource resource = (ContentResource)child;

                // Use JAXB to parse the item
                Reader r = new InputStreamReader(resource.getInputStream());
                XAppRegistryItem item = XAppRegistryItem.decode(r);

                if (item.getAppName().equalsIgnoreCase(checkApp)) {
                    response.setContentType("text/plain");
View Full Code Here

    public static AvatarConfigSettings loadConfigSettings(ContentCollection c)
            throws JAXBException, ContentRepositoryException {

        // Check to see if the CONFIG_FILENAME file resource exists, log an
        // error and return a new AvatarConfigSettings object if necessary.
        ContentResource resource = (ContentResource) c.getChild(CONFIG_FILENAME);
        if (resource == null) {
            logger.warning("Unable to find " + CONFIG_FILENAME + " in " + c.getPath());
            return new AvatarConfigSettings();
        }

        Reader r = new InputStreamReader(resource.getInputStream());
        return AvatarConfigSettings.decode(r);
    }
View Full Code Here

    public static void saveConfigSettings(ContentCollection c, AvatarConfigSettings settings)
            throws JAXBException, ContentRepositoryException {

        // Check to see if the CONFIG_FILENAME file resource exists, creating
        // it if necessary
        ContentResource resource = (ContentResource)c.getChild(CONFIG_FILENAME);
        if (resource == null) {
            resource = (ContentResource)c.createChild(CONFIG_FILENAME, Type.RESOURCE);
        }

        // Write the given settings as an XML stream to the output file
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Writer w = new OutputStreamWriter(os);
        settings.encode(w);
        resource.put(os.toByteArray());
    }
View Full Code Here

TOP

Related Classes of org.jdesktop.wonderland.modules.contentrepo.common.ContentResource

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.