001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.repository.impl;
018:
019: import java.io.IOException;
020:
021: import org.apache.avalon.framework.activity.Disposable;
022: import org.apache.avalon.framework.component.Component;
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.Serviceable;
027: import org.apache.cocoon.components.repository.helpers.CredentialsToken;
028: import org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper;
029: import org.apache.cocoon.components.webdav.WebDAVUtil;
030: import org.apache.commons.httpclient.HttpException;
031:
032: /**
033: * A transaction an locking helper class
034: * intended to be used by flowscripts or corresponding wrapper components.
035: */
036: public class WebDAVRepositoryTransactionHelper extends
037: AbstractLogEnabled implements RepositoryTransactionHelper,
038: Serviceable, Disposable, Component {
039:
040: /* The ServiceManager */
041: private ServiceManager manager;
042:
043: /* The repository component */
044: private WebDAVRepository repo;
045:
046: /* The credentials to be used against the WebDAV repository */
047: private CredentialsToken credentials;
048:
049: /* (non-Javadoc)
050: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
051: */
052: public void service(ServiceManager manager) throws ServiceException {
053: this .manager = manager;
054: }
055:
056: /* (non-Javadoc)
057: * @see org.apache.avalon.framework.activity.Disposable#dispose()
058: */
059: public void dispose() {
060: this .manager = null;
061: }
062:
063: /**
064: * create a WebDAVRepositoryTransactionHelper
065: *
066: * @param credentials the user credentials to be used against the WebDAV repository.
067: * @param repo a reference to the WebDAVRepository object.
068: */
069: public WebDAVRepositoryTransactionHelper(
070: CredentialsToken credentials, WebDAVRepository repo) {
071: this .credentials = credentials;
072: this .repo = repo;
073: }
074:
075: /* (non-Javadoc)
076: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#beginTran()
077: */
078: public boolean beginTran() {
079: //may be implemented via DeltaV activities?
080: throw new UnsupportedOperationException();
081: }
082:
083: /* (non-Javadoc)
084: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#commitTran()
085: */
086: public boolean commitTran() {
087: //may be implemented via DeltaV activities?
088: throw new UnsupportedOperationException();
089: }
090:
091: /* (non-Javadoc)
092: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#rollbackTran()
093: */
094: public boolean rollbackTran() {
095: //may be implemented via DeltaV activities?
096: throw new UnsupportedOperationException();
097: }
098:
099: /* (non-Javadoc)
100: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#lock(java.lang.String)
101: */
102: public boolean lock(String uri) {
103:
104: try {
105: return WebDAVUtil.getWebdavResource(
106: this .repo.getAbsoluteURI(uri)).lockMethod();
107:
108: } catch (HttpException he) {
109: this .getLogger().error("HTTP Error locking " + uri, he);
110: } catch (IOException ioe) {
111: this .getLogger().error("IO Error locking " + uri, ioe);
112: }
113:
114: return false;
115: }
116:
117: /* (non-Javadoc)
118: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#lock(java.lang.String, int)
119: */
120: public boolean lock(String uri, int timeout) {
121:
122: try {
123: return WebDAVUtil.getWebdavResource(
124: this .repo.getAbsoluteURI(uri)).lockMethod(
125: this .credentials.getPrincipal().getName(), timeout);
126:
127: } catch (HttpException he) {
128: this .getLogger().error("HTTP Error locking " + uri, he);
129: } catch (IOException ioe) {
130: this .getLogger().error("IO Error locking " + uri, ioe);
131: }
132:
133: return false;
134: }
135:
136: /* (non-Javadoc)
137: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#unlock(java.lang.String)
138: */
139: public boolean unlock(String uri) {
140:
141: try {
142: return WebDAVUtil.getWebdavResource(
143: this .repo.getAbsoluteURI(uri)).unlockMethod();
144:
145: } catch (HttpException he) {
146: this .getLogger().error("HTTP Error unlocking " + uri, he);
147: } catch (IOException ioe) {
148: this .getLogger().error("IO Error unlocking " + uri, ioe);
149: }
150:
151: return false;
152: }
153:
154: /* (non-Javadoc)
155: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#supportsTransactions()
156: */
157: public boolean supportsTransactions() {
158: //may be implemeted via DeltaV activities? --> make configurable
159: return false;
160: }
161:
162: /* (non-Javadoc)
163: * @see org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper#supportsLocking()
164: */
165: public boolean supportsLocking() {
166: return true;
167: }
168:
169: }
|