001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/course-management/tags/sakai_2-4-1/cm-impl/hibernate-impl/impl/src/java/org/sakaiproject/coursemanagement/impl/job/ClassPathCMSyncJob.java $
003: * $Id: ClassPathCMSyncJob.java 15123 2006-09-22 00:45:08Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.coursemanagement.impl.job;
021:
022: import java.io.InputStream;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.quartz.Job;
027: import org.quartz.JobExecutionContext;
028: import org.quartz.JobExecutionException;
029: import org.sakaiproject.authz.cover.AuthzGroupService;
030: import org.sakaiproject.event.cover.EventTrackingService;
031: import org.sakaiproject.event.cover.UsageSessionService;
032: import org.sakaiproject.tool.api.Session;
033: import org.sakaiproject.tool.cover.SessionManager;
034:
035: /**
036: * A sample quartz job to synchronize the CM data in Sakai's hibernate impl with an
037: * xml file available in the classpath.
038: *
039: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
040: *
041: */
042: public class ClassPathCMSyncJob extends CmSynchronizer implements Job {
043: private static final Log log = LogFactory
044: .getLog(ClassPathCMSyncJob.class);
045:
046: protected String classPathToXml;
047:
048: public void init() {
049: if (log.isInfoEnabled())
050: log.info("init()");
051: }
052:
053: public void destroy() {
054: if (log.isInfoEnabled())
055: log.info("destroy()");
056: }
057:
058: /**
059: * {@inheritDoc}
060: */
061: public InputStream getXmlInputStream() {
062: return getClass().getClassLoader().getResourceAsStream(
063: classPathToXml);
064: }
065:
066: /**
067: * {@inheritDoc}
068: */
069: public void execute(JobExecutionContext arg0)
070: throws JobExecutionException {
071: loginToSakai();
072: syncAllCmObjects();
073: logoutFromSakai();
074: }
075:
076: protected void loginToSakai() {
077: Session sakaiSession = SessionManager.getCurrentSession();
078: sakaiSession.setUserId("admin");
079: sakaiSession.setUserEid("admin");
080:
081: // establish the user's session
082: UsageSessionService
083: .startSession("admin", "127.0.0.1", "CMSync");
084:
085: // update the user's externally provided realm definitions
086: AuthzGroupService.refreshUser("admin");
087:
088: // post the login event
089: EventTrackingService.post(EventTrackingService.newEvent(
090: UsageSessionService.EVENT_LOGIN, null, true));
091: }
092:
093: protected void logoutFromSakai() {
094: // post the logout event
095: EventTrackingService.post(EventTrackingService.newEvent(
096: UsageSessionService.EVENT_LOGOUT, null, true));
097: }
098:
099: // Dependency Injection
100: public void setClassPathToXml(String classPathToXml) {
101: this.classPathToXml = classPathToXml;
102: }
103: }
|