castorpsmlmanagerservice.java

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

JAVA
1,570
字号
                                             state);
            subQuery(qs, absPath);
        }

        return list.iterator();
    }

    /** Create a profile based on import flag.
     *
     */
    protected Profile createProfile()
    {
        if (importFlag)
            return new ImportProfile(this, this.consumer);
        else
            return Profiler.createProfile();
    }

    protected Profile createProfile(ProfileLocator locator)
    {
        if (importFlag)
            return new ImportProfile(this, this.consumer, locator);
        else
            return Profiler.createProfile(locator);
    }

    /** Query for a collection of profiles given a profile locator criteria.
     *  This method should be used when importing or exporting profiles between services.
     *
     * @param locator The profile locator criteria.
     * @return The count of profiles exported.
     */
    public int export(PsmlManagerService consumer, QueryLocator locator)
    {
        importFlag = true;
        Iterator profiles = null;
        int count = 0;
        try
        {
            this.consumer = consumer;
            profiles = query(locator);

            while (profiles.hasNext() )
            {
                Profile profile = (Profile)profiles.next();
                //dumpProfile(profile);
                try
                {
                    consumer.createDocument(profile);
                    count++;
                }
                catch (Exception ex)
                {
                    try
                    {
                        consumer.store(profile);
                        count++;
                    }
                    catch (Exception e)
                    {
                        logger.error("PSMLManager: Failed to export profiles to DB: " + profile, ex );
                    }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            logger.error("PSMLManager: Failed to export profiles to DB: " , e );

        }
        finally
        {
            importFlag = false;
        }
        return count;
    }


    /** Query for a collection of profiles given a profile locator criteria.
     *  To specify 'all' - use '*' in the criteria
     *
     * @param locator The profile locator criteria.
     */
    protected void subQuery(QueryState qs, String path)
    {
        File file = new File(path);
        if (file.isFile())
        {
            try
            {
                String filename = file.getName();
                if (!filename.endsWith(this.ext))
                    return;

                Profile clone = (Profile)qs.profile.clone();
                clone.setName(filename);
                qs.list.add( clone );
            }
            catch (Exception e)
            {
                logger.error("PSMLManager: Failed to clone profile: " + path + " : " + e, e);
            }
        }
        else if (file.isDirectory())
        {
            String dirName = file.getName();
            qs.state++;

            // filter out based on name, mediatype, language, country
            if (qs.state == STATE_NAME)
            {
                if (null != qs.name)
                {
                    if (!dirName.equals(qs.name))
                        return;
                }
                try
                {
                    if (QUERY_BY_USER == qs.queryBy)
                    {
                        JetspeedUser user = (JetspeedUser)qs.profile.getUser();
                        if (null == user)
                        {
                            user = JetspeedUserFactory.getInstance();
                            user.setUserName(file.getName());
                            qs.profile.setUser(user);
                            qs.clearName = true;
                        }
                    }
                    else if (QUERY_BY_ROLE == qs.queryBy)
                    {
                        Role role = qs.profile.getRole();
                        if (null == role)
                        {
                            role = JetspeedRoleFactory.getInstance();
                            role.setName(file.getName());
                            qs.profile.setRole(role);
                            qs.clearName = true;
                        }
                    }
                    else if (QUERY_BY_GROUP == qs.queryBy)
                    {
                        Group group = qs.profile.getGroup();
                        if (null == group)
                        {
                            group = JetspeedGroupFactory.getInstance();
                            group.setName(file.getName());
                            qs.profile.setGroup(group);
                            qs.clearName = true;
                        }
                    }
                }
                catch (Exception e)
                {}


            }
            else if (qs.state == STATE_MEDIA)
            {
                String media = qs.locator.getMediaType();
                if (null != media)
                {
                    if (!dirName.equals(media))
                        return;
                }
                else
                {
                    qs.profile.setMediaType(dirName);
                    qs.clearMedia = true;
                }
            }
            else if (qs.state == STATE_LANGUAGE)
            {
                String language = qs.locator.getLanguage();
                if (null != language)
                {
                    if (!dirName.equals(language))
                        return;
                }
                else
                {
                    qs.profile.setLanguage(dirName);
                    qs.clearLanguage = true;
                }
            }
            else if (qs.state == STATE_COUNTRY)
            {
                String country = qs.locator.getCountry();
                if (null != country)
                {
                    if (!dirName.equals(country))
                        return;
                }
                else
                {
                    qs.profile.setCountry(dirName);
                    qs.clearCountry = true;
                }
            }

            if (!path.endsWith(File.separator))
                path += File.separator;

            String files[] = file.list();


            // Process all files recursivly
            for(int ix = 0; files != null && ix < files.length; ix++)
            {
                subQuery(qs, path + files[ix]);
            }

            // clear state
            if (qs.state == STATE_NAME && true == qs.clearName)
            {
                if (QUERY_BY_USER == qs.queryBy)
                    qs.profile.setUser(null);
                else if (QUERY_BY_ROLE == qs.queryBy)
                    qs.profile.setRole(null);
                else if (QUERY_BY_GROUP == qs.queryBy)
                    qs.profile.setGroup(null);
                qs.clearName = false;
            }
            else if (qs.state == STATE_MEDIA && true == qs.clearMedia)
            {
                qs.profile.setMediaType(null);
                qs.clearMedia = false;
            }
            else if (qs.state == STATE_LANGUAGE && true == qs.clearLanguage)
            {
                qs.profile.setLanguage(null);
                qs.clearLanguage = false;
            }
            else if (qs.state == STATE_COUNTRY && true == qs.clearCountry)
            {
                qs.profile.setCountry(null);
                qs.clearCountry = false;
            }

            qs.state--;

        }

    }

     static int QUERY_BY_USER = 0;
     static int QUERY_BY_ROLE = 1;
     static int QUERY_BY_GROUP = 2;

    protected class QueryState
    {

        QueryState( int queryBy,
                    Profile profile,
                    ProfileLocator locator,
                    List list,
                    String name,
                    int state)
        {
            this.queryBy = queryBy;
            this.profile = profile;
            this.locator = locator;
            this.list = list;
            this.name = name;
            this.state = state;
        }

        protected int queryBy;
        protected Profile profile;
        protected ProfileLocator locator;
        protected List list;
        protected String name;
        protected int state;

        protected boolean clearName = false;
        protected boolean clearMedia = false;
        protected boolean clearLanguage = false;
        protected boolean clearCountry = false;

    }

    protected void testCases()
    {
        try
        {
            QueryLocator locator = new QueryLocator( QueryLocator.QUERY_USER );
            Iterator x1 = query( locator );
            dump( x1 );

            QueryLocator locator2 = new QueryLocator( QueryLocator.QUERY_USER );
            locator2.setUser( JetspeedSecurity.getUser("turbine") );
            Iterator x2 = query( locator2 );
            dump( x2 );


            QueryLocator locator4 = new QueryLocator( QueryLocator.QUERY_GROUP );
//            locator4.setGroup( JetspeedSecurity.getGroup("apache") );
            Iterator x4 = query( locator4 );
            dump( x4 );
          }
        catch (Exception e)
        {
            System.out.println( "Exception in Debug:" + e);
        }
    }

    protected void dump( Iterator it )
    {
        System.out.println("===============================================");
        while (it.hasNext() )
        {
            Profile profile = (Profile)it.next();
            dumpProfile(profile);
        }
        System.out.println("===============================================");
    }

    protected void dumpProfile(Profile profile)
    {
        JetspeedUser user = profile.getUser();
        Group group = profile.getGroup();
        Role role = profile.getRole();
        if (profile.getAnonymous() == true)
            System.out.println("ANON USER");
        System.out.println("RESOURCE = " + profile.getName());
        if (null != user)
            System.out.println("USER = " + user.getUserName() );
        if (null != group)
            System.out.println("GROUP = " + group.getName() );
        if (null != role)
            System.out.println("ROLE = " + role.getName() );
        System.out.println("MEDIA TYPE = " + profile.getMediaType());
        System.out.println("LANGUAGE = " + profile.getLanguage());
        System.out.println("COUNTRY = " + profile.getCountry());
        PSMLDocument doc = profile.getDocument();
        if (null == doc)
            System.out.println("Document is null");
        else
        {
            if (null == profile.getName())
                System.out.println("profile name is null");
            else
                System.out.println("Doc.name=" + profile.getName());
        }

        System.out.println("----------------------");
    }

    /**
     * Refresh event, called when the entry is being refreshed from file system.
     *
     * @param entry the entry being refreshed.
     */
    public void refresh(FileCacheEntry entry)
    {
        if (logger.isInfoEnabled())
        {
            logger.info("CastorPsmlManager: Entry is refreshing: " + entry.getFile().getPath());
        }

        Profile profile = (Profile) entry.getDocument();
        String path = null;

        if (profile != null)
        {
            try
            {
                path = entry.getFile().getCanonicalPath();
                profile.setDocument(loadDocument(path));
            }
            catch(java.io.IOException e)
            {
                logger.error("CastorPsmlManager: Failed to refresh document "+path);
            }
        }
    }

    /**
     * Evict event, called when the entry is being evicted out of the cache
     *
     * @param entry the entry being refreshed.
     */
    public void evict(FileCacheEntry entry)
    {
        System.out.println("entry is evicting: " + entry.getFile().getName());
    }

}

⌨️ 快捷键说明

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