castorpsmlmanagerservice.java

来自「jetspeed源代码」· Java 代码 · 共 1,570 行 · 第 1/4 页

JAVA
1,570
字号
            documents.remove(name);
        }

        file.delete();

    }

    /** Removes all documents for a given user.
     *
     * @param user The user object.
     */
    public void removeUserDocuments( JetspeedUser user )
    {
        ProfileLocator locator = Profiler.createLocator();
        locator.setUser(user);
        StringBuffer buffer = new StringBuffer();
        buffer.append(PATH_USER);
        String name = user.getUserName();
        if (null != name && name.length() > 0)
        {
            buffer.append(File.separator)
                .append(name);
        }
        else
            return; // don't delete the entire user directories

        String path = buffer.toString();
        File base = this.rootDir;
        File file = new File(base, path);

        try
        {
            name = file.getCanonicalPath();
        }
        catch (IOException e)
        {
            logger.error("PSMLManager: unable to resolve file path for "+ file);
        }


        synchronized (documents)
        {
            DirectoryUtils.rmdir(name);
            Iterator it = documents.getIterator();
            while (it.hasNext())
            {
                FileCacheEntry entry = (FileCacheEntry)it.next();
                if (null == entry)
                {
                    continue;
                }
                Profile profile = (Profile)entry.getDocument();
                if (null == profile)
                {
                    continue;
                }
                JetspeedUser pUser = profile.getUser();
                if (null != pUser && pUser.getUserName().equals(user.getUserName()))
                {
                    documents.remove(profile.getDocument().getName());
                }
            }
        }

    }

    /** Removes all documents for a given role.
     *
     * @param role The role object.
     */
    public void removeRoleDocuments( Role role )
    {
        ProfileLocator locator = Profiler.createLocator();
        locator.setRole(role);
        StringBuffer buffer = new StringBuffer();
        buffer.append(PATH_ROLE);
        String name = role.getName();
        if (null != name && name.length() > 0)
        {
            buffer.append(File.separator)
                .append(name);
        }
        else
            return; // don't delete the entire role directories

        String path = buffer.toString();
        File base = this.rootDir;
        File file = new File(base, path);

        try
        {
            name = file.getCanonicalPath();
        }
        catch (IOException e)
        {
            logger.error("PSMLManager: unable to resolve file path for "+ file);
        }


        synchronized (documents)
        {
            DirectoryUtils.rmdir(name);
            Iterator it = documents.getIterator();
            while (it.hasNext())
            {
                FileCacheEntry entry = (FileCacheEntry)it.next();
                if (null == entry)
                {
                    continue;
                }
                Profile profile = (Profile)entry.getDocument();
                if (null == profile)
                {
                    continue;
                }
                Role pRole = profile.getRole();
                if (null != pRole && pRole.getName().equals(role.getName()))
                {
                    documents.remove(profile.getDocument().getName());
                }
            }
        }
    }

    /** Removes all documents for a given group.
     *
     * @param group The group object.
     */
    public void removeGroupDocuments( Group group )
    {
        ProfileLocator locator = Profiler.createLocator();
        locator.setGroup(group);
        StringBuffer buffer = new StringBuffer();
        buffer.append(PATH_GROUP);
        String name = group.getName();
        if (null != name && name.length() > 0)
        {
            buffer.append(File.separator)
                .append(name);
        }
        else
            return; // don't delete the entire group directories

        String path = buffer.toString();
        File base = this.rootDir;
        File file = new File(base, path);

        try
        {
            name = file.getCanonicalPath();
        }
        catch (IOException e)
        {
            logger.error("PSMLManager: unable to resolve file path for "+ file);
        }


        synchronized (documents)
        {
            DirectoryUtils.rmdir(name);
            Iterator it = documents.getIterator();
            while (it.hasNext())
            {
                FileCacheEntry entry = (FileCacheEntry)it.next();
                if (null == entry)
                {
                    continue;
                }
                Profile profile = (Profile)entry.getDocument();
                if (null == profile)
                {
                    continue;
                }
                Group pGroup = profile.getGroup();
                if (null != pGroup && pGroup.getName().equals(group.getName()))
                {
                    documents.remove(profile.getDocument().getName());
                }
            }
        }

    }


    /**
     * Maps a ProfileLocator to a file.
     *
     * @param locator The profile locator describing the PSML resource to be found.
     * @return the String path of the file.
     */
    protected String mapLocatorToFile(ProfileLocator locator)
    {
        StringBuffer path = new StringBuffer();

        // move the base dir is either user or role is specified
        Role role = locator.getRole();
        Group group = locator.getGroup();
        JetspeedUser user = locator.getUser();

        if (user != null)
        {
            path.append(PATH_USER);
            String name = user.getUserName();
            if (null != name && name.length() > 0)
            {
                path.append(File.separator)
                    .append(name);
            }
        }
        else if (group != null)
        {
            path.append(PATH_GROUP);
            String name = group.getName();
            if (null != name && name.length() > 0)
            {
                path.append(File.separator)
                    .append(name);
            }
        }
        else if (null != role)
        {
            path.append(PATH_ROLE);
            String name = role.getName();
            if (null != name && name.length() > 0)
            {
                path.append(File.separator)
                    .append(name);
            }
        }

        // Media
        if (null != locator.getMediaType())
        {
            path.append(File.separator)
                .append(locator.getMediaType());
        }
        // Language
        if (null != locator.getLanguage() && (! locator.getLanguage().equals("-1")))
        {
            path.append(File.separator)
                .append(locator.getLanguage());
        }
        // Country
        if (null != locator.getCountry() && (! locator.getCountry().equals("-1")))
        {
            path.append(File.separator)
                .append(locator.getCountry());
        }
        // Resource Name
        if (null != locator.getName())
        {
            if (!(locator.getName().endsWith(CastorPsmlManagerService.DEFAULT_EXT)))
            {
                path.append(File.separator)
                    .append(locator.getName()).append(CastorPsmlManagerService.DEFAULT_EXT);
            }
            else
            {
                path.append(File.separator)
                    .append(locator.getName());
            }
        }
        else
        {
            path.append(File.separator)
                .append(DEFAULT_RESOURCE);
        }

        return  path.toString();
    }

    protected static int STATE_INIT = 0;
    protected static int STATE_BASE = 1;
    protected static int STATE_NAME = 2;
    protected static int STATE_MEDIA = 3;
    protected static int STATE_LANGUAGE = 4;
    protected static int STATE_COUNTRY = 5;

    /** Query for a collection of profiles given a profile locator criteria.
     *
     * @param locator The profile locator criteria.
     */
    public Iterator query( QueryLocator locator )
    {
        List list = new LinkedList();

        Role role = locator.getRole();
        Group group = locator.getGroup();
        JetspeedUser user = locator.getUser();

        // search thru anonymous directories?
        int qm = locator.getQueryMode();
        if ((qm & QueryLocator.QUERY_USER) == QueryLocator.QUERY_USER)
        {
            Profile profile = createProfile();
            StringBuffer path = new StringBuffer();
            path.append(PATH_USER);
            String name = null;
            int state = STATE_INIT;
            if (null != user)
            {
                name = user.getUserName();
                profile.setUser( user );
                if (null != name)
                {
                    path.append(File.separator).append(name);
                    state = STATE_BASE;
                }
            }
            File base = this.rootDir;
            File file = new File(base, path.toString());
            String absPath = file.getAbsolutePath();
            QueryState qs = new QueryState( QUERY_BY_USER,
                                             profile,
                                             locator,
                                             list,
                                             name,
                                             state);
            subQuery(qs, absPath);
        }
        if ((qm & QueryLocator.QUERY_ROLE) == QueryLocator.QUERY_ROLE)
        {
            Profile profile = createProfile();
            StringBuffer path = new StringBuffer();
            path.append(PATH_ROLE);
            String name = null;
            int state = STATE_INIT;
            if (null != role)
            {
                name = role.getName();
                profile.setRole( role );
                if (null != name)
                {
                    path.append(File.separator).append(name);
                    state = STATE_BASE;
                }
            }
            File base = this.rootDir;
            File file = new File(base, path.toString());
            String absPath = null;

            try
            {
                absPath = file.getCanonicalPath();
            }
            catch (IOException e)
            {
                logger.error("PSMLManager: unable to resolve file path for "+ file);
            }

            QueryState qs = new QueryState( QUERY_BY_ROLE,
                                             profile,
                                             locator,
                                             list,
                                             name,
                                             state);
            subQuery(qs, absPath);
        }
        if ((qm & QueryLocator.QUERY_GROUP) == QueryLocator.QUERY_GROUP)
        {
            Profile profile = createProfile();
            StringBuffer path = new StringBuffer();
            path.append(PATH_GROUP);
            String name = null;
            int state = STATE_INIT;
            if (null != group)
            {
                name = group.getName();
                profile.setGroup( group );
                if (null != name)
                {
                    path.append(File.separator).append(name);
                    state = STATE_BASE;
                }
            }
            File base = this.rootDir;
            File file = new File(base, path.toString());
            String absPath = null;

            try
            {
                absPath = file.getCanonicalPath();
            }
            catch (IOException e)
            {
                logger.error("PSMLManager: unable to resolve file path for "+ file);
            }

            QueryState qs = new QueryState( QUERY_BY_GROUP,
                                             profile,
                                             locator,
                                             list,
                                             name,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?