databasepsmlmanagerservice.java

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

JAVA
1,277
字号
     * @param page page name for this resource
     * @return PSMLDocument object for given page and portlets
     */
    private PSMLDocument getPSMLDocument(String page, Portlets portlets)
    {
        PSMLDocument psmldoc = new BasePSMLDocument();
        psmldoc.setName(page);
        psmldoc.setPortlets(portlets);
        return psmldoc;
    }


    /**
     * Given ordered list of locators, find the first document matching
     * a profile locator, starting from the beginning of the list and working
     * to the end.
     *
     * @param locator The ordered list of profile locators.
     * @return PSMLDocument object for the first document matching a locator
     */
    public PSMLDocument getDocument(List locators)
    {
        if (locators == null)
        {
            String message = "PSMLManager: Must specify a list of locators";
            logger.warn("DatabasePsmlManagerService.getDocument: " + message);
            throw new IllegalArgumentException(message);
        }

        // iterate over the list and invoke getDocument(locator) method
        for (int i = 0; i < locators.size(); i++)
        {
            PSMLDocument psmldoc = getDocument((ProfileLocator)locators.get(i));
            if (psmldoc != null)
            {
                return psmldoc;
            }
        }
        return null;
    }

    /**
     * Returns a PSML document for the given locator, it is called by the cache
     * refresher
     *
     * @param locator The locator descriptor(ProfileLocator object) of the
     * document to be retrieved.
     * @return psmldoc The PSMLDocument object
     */
    public PSMLDocument refresh(ProfileLocator locator)
    {
        // go to database and get the blob, and marshal the Portlets

        if (locator == null)
        {
            String message = "PSMLManager: Must specify a locator";
            logger.warn("DatabasePsmlManagerService.refresh: " + message);
            throw new IllegalArgumentException(message);
        }

        JetspeedUser user = locator.getUser();
        Role role = locator.getRole();
        Group group = locator.getGroup();
        String tableName = null;
        List records = null;
        Portlets portlets = null;
        PSMLDocument psmldoc = null;
        String page = null;

        Connection dbCon = getDbConnection();

        try
        {
            if (user != null)
            {
                tableName = "JETSPEED_USER_PROFILE";
                records = new JetspeedUserProfilePeer().select(locator, dbCon);
                Iterator iterator = records.iterator();
                while (iterator.hasNext())
                {
                    JetspeedUserProfile uprofile =
                                       (JetspeedUserProfile)iterator.next();
                    page = uprofile.getPage();
                    portlets = DBUtils.bytesToPortlets(uprofile.getProfile(), this.mapping);
                }
            }
            else if (role != null)
            {
                tableName = "JETSPEED_ROLE_PROFILE";
                records = new JetspeedRoleProfilePeer().select(locator, dbCon);
                Iterator iterator = records.iterator();
                while (iterator.hasNext())
                {
                    JetspeedRoleProfile rprofile =
                                       (JetspeedRoleProfile)iterator.next();
                    page = rprofile.getPage();
                    portlets = DBUtils.bytesToPortlets(rprofile.getProfile(), this.mapping);
                }
            }
            else if (group != null)
            {
                tableName = "JETSPEED_GROUP_PROFILE";
                records = new JetspeedGroupProfilePeer().select(locator, dbCon);
                Iterator iterator = records.iterator();
                while (iterator.hasNext())
                {
                    JetspeedGroupProfile gprofile =
                                       (JetspeedGroupProfile)iterator.next();
                    page = gprofile.getPage();
                    portlets = DBUtils.bytesToPortlets(gprofile.getProfile(), this.mapping);
                }
            }

            if (page != null && portlets != null)
            {
                psmldoc = getPSMLDocument(page, portlets);
                if (cachingOn)
                {
                    synchronized (psmlCache)
                    {
                        if (logger.isDebugEnabled())
                            logger.debug("DatabasePsmlManagerService.refresh: caching document: profile: " + locatorToString(locator));
                        psmlCache.put(locatorToString(locator), psmldoc);
                    }
                }
                return psmldoc;
            }
            else
            {
                if (cachingOn)
                {
                    // cache the fact that there is NO document matching this profile
                    psmlCache.put(locatorToString(locator), null);
                    if (logger.isDebugEnabled())
                        logger.debug("DatabasePsmlManagerService.refresh: caching 'document not found': profile: " + locatorToString(locator));
                }
            }
        }
        catch (Exception e)
        {
            logger.warn("DatabasePsmlManagerService.refresh: profile: " + locatorToString(locator)
                + " tableName: " + tableName, e);
            throw new RuntimeException("Could not refresh profile from DB");
        }
        finally
        {
            // make sure to release the database connection
            releaseDbConnection(dbCon);
        }

        if (logger.isDebugEnabled())
            logger.debug("DatabasePsmlManagerService.refresh: no document found: profile: "
                    + locatorToString(locator));
        return null;
    }

    /** Removes all documents for a given user.
     *
     * @param user The user object.
     */
    public void removeUserDocuments(JetspeedUser user)
    {
        Connection dbCon = getDbConnection();

        try
        {
            if (user != null)
            {
                new JetspeedUserProfilePeer().delete(user, dbCon);
            }
        }
        catch (Exception e) // delete failed
        {
            logger.warn("DatabasePsmlManagerService.removeUserDocuments: exception:", e);
            throw new RuntimeException("Could not delete documents for given user from DB");
        }
        finally
        {
            // make sure to release the database connection
            releaseDbConnection(dbCon);
        }

    }

    /** Removes all documents for a given role.
     *
     * @param role The role object.
     */
    public void removeRoleDocuments(Role role)
    {
        Connection dbCon = getDbConnection();

        try
        {
            if (role != null)
            {
                new JetspeedRoleProfilePeer().delete(role, dbCon);
            }
        }
        catch (Exception e) // delete failed
        {
            logger.warn("DatabasePsmlManagerService.removeRoleDocuments: exception:", e);
            throw new RuntimeException("Could not delete documents for given role from DB");
        }
        finally
        {
            // make sure to release the database connection
            releaseDbConnection(dbCon);
        }
    }

    /** Removes all documents for a given group.
     *
     * @param group The group object.
     */
    public void removeGroupDocuments(Group group)
    {
        Connection dbCon = getDbConnection();

        try
        {
            if (group != null)
            {
                new JetspeedGroupProfilePeer().delete(group, dbCon);
            }
        }
        catch (Exception e) // delete failed
        {
            logger.warn("DatabasePsmlManagerService.removeGroupDocuments: exception:", e);
            throw new RuntimeException("Could not delete documents for given group from DB");
        }
        finally
        {
            // make sure to release the database connection
            releaseDbConnection(dbCon);
        }
    }

    /** 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)
    {
        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.warn("DatabasePsmlManagerService.export: profile: "
                                + profile, ex);
                    }
                }
            }
        }
        catch(Exception e)
        {
            logger.warn("DatabasePsmlManagerService.export: exception:", e);

        }
        finally
        {
        }
        return count;
    }


    public Mapping getMapping()
    {
        return this.mapping;
    }

    /**
     * Creates a user profile from a JetspeedUserProfile database object.
     *
     * @param entity The user profile entity in the database.
     * @param portlets The PSML blob.
     * @return A new profile object representing the locator and PSML blob.
     */
    public Profile createUserProfile(JetspeedUserProfile entity, Portlets portlets)
    {
        Profile profile = Profiler.createProfile();
        try
        {
            JetspeedUser user = JetspeedSecurity.getUser(entity.getUserName());
            if (null == user)
            {
                user = JetspeedUserFactory.getInstance();
                user.setUserName(entity.getUserName());
            }
            profile.setUser(user);

            profile.setMediaType(entity.getMediaType());
            profile.setLanguage(entity.getLanguage());
            profile.setCountry(entity.getCountry());
            profile.setName(entity.getPage());
            profile.setDocument(getPSMLDocument(entity.getPage(), portlets));
        }
        catch (JetspeedSecurityException e)
        {
        }
        return profile;
    }

    /**
     * Creates a group profile from a JetspeedGroupProfile database object.
     *
     * @param entity The group profile entity in the database.
     * @param portlets The PSML blob.
     * @return A new profile object representing the locator and PSML blob.
     */
    public Profile createGroupProfile(JetspeedGroupProfile entity, Portlets portlets)
    {
        Profile profile = Profiler.createProfile();
        try
        {
            Group group = JetspeedSecurity.getGroup(entity.getGroupName());
            if (null == group)
            {
                group = JetspeedGroupFactory.getInstance();
                group.setName(entity.getGroupName());
            }
            profile.setGroup(group);
            profile.setMediaType(entity.getMediaType());
            profile.setLanguage(entity.getLanguage());
            profile.setCountry(entity.getCountry());
            profile.setName(entity.getPage());
            profile.setDocument(getPSMLDocument(entity.getPage(), portlets));
        }
        catch (JetspeedSecurityException e)
        {
        }
        return profile;
    }

    /**
     * Creates a role profile from a JetspeedRoleProfile database object.
     *
     * @param entity The group profile entity in the database.
     * @param portlets The PSML blob.
     * @return A new profile object representing the locator and PSML blob.
     */
    public Profile createRoleProfile(JetspeedRoleProfile entity, Portlets portlets)
    {
        Profile profile = Profiler.createProfile();
        try
        {
            Role role = JetspeedSecurity.getRole(entity.getRoleName());
            if (null == role)
            {
                role = JetspeedRoleFactory.getInstance();
                role.setName(entity.getRoleName());
            }
            profile.setRole(role);
            profile.setMediaType(entity.getMediaType());
            profile.setLanguage(entity.getLanguage());
            profile.setCountry(entity.getCountry());
            profile.setName(entity.getPage());
            profile.setDocument(getPSMLDocument(entity.getPage(), portlets));
        }
        catch (JetspeedSecurityException e)
        {
        }
        return profile;
    }


    /**
    * Get a database connection to the default or specifed torque database pool
    */
    private Connection getDbConnection()
    {
        try
        {
            // use the default pool if not specified
            if (poolName == null)
            {
                return Torque.getConnection();
            }

            // otherwise use the specified pool name
            else
            {
                return Torque.getConnection(poolName);
            }
        }
        catch (Exception e)
        {
            logger.warn("DatabasePsmlManagerService.getDbConnection: exception: " + e);
            return null;
        }
    }

    /**
    * Release a previously gotten database connection back to the torque pool
    */
    private void releaseDbConnection(Connection connection)
    {
        Torque.closeConnection(connection);
    }
}

⌨️ 快捷键说明

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