]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AbstractUrlProxy.java
7692163e3a008a5ea4b70f71fc34e9933c68c93f
[lgpl/argeo-commons.git] / AbstractUrlProxy.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr.proxy;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21
22 import javax.jcr.Binary;
23 import javax.jcr.Node;
24 import javax.jcr.Property;
25 import javax.jcr.Repository;
26 import javax.jcr.RepositoryException;
27 import javax.jcr.Session;
28 import javax.jcr.nodetype.NodeType;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.argeo.ArgeoException;
34 import org.argeo.jcr.JcrUtils;
35
36 /** Base class for URL based proxys. */
37 public abstract class AbstractUrlProxy implements ResourceProxy {
38 private final static Log log = LogFactory.getLog(AbstractUrlProxy.class);
39
40 private Repository jcrRepository;
41 private Session jcrAdminSession;
42 private String proxyWorkspace = "proxy";
43
44 protected abstract Node retrieve(Session session, String path);
45
46 void init() {
47 try {
48 jcrAdminSession = JcrUtils.loginOrCreateWorkspace(jcrRepository,
49 proxyWorkspace);
50 beforeInitSessionSave(jcrAdminSession);
51 if (jcrAdminSession.hasPendingChanges())
52 jcrAdminSession.save();
53 } catch (Exception e) {
54 JcrUtils.discardQuietly(jcrAdminSession);
55 throw new ArgeoException("Cannot initialize Maven proxy", e);
56 }
57 }
58
59 /**
60 * Called before the (admin) session is saved at the end of the
61 * initialization. Does nothing by default, to be overridden.
62 */
63 protected void beforeInitSessionSave(Session session)
64 throws RepositoryException {
65 }
66
67 void destroy() {
68 JcrUtils.logoutQuietly(jcrAdminSession);
69 }
70
71 /**
72 * Called before the (admin) session is logged out when resources are
73 * released. Does nothing by default, to be overridden.
74 */
75 protected void beforeDestroySessionLogout() throws RepositoryException {
76 }
77
78 public Node proxy(String path) {
79 // we open a JCR session with client credentials in order not to use the
80 // admin session in multiple thread or make it a bottleneck.
81 Session clientSession = null;
82 try {
83 clientSession = jcrRepository.login(proxyWorkspace);
84 if (!clientSession.itemExists(path)
85 || shouldUpdate(clientSession, path)) {
86 Node nodeT = retrieveAndSave(path);
87 if (nodeT == null)
88 return null;
89 }
90 return clientSession.getNode(path);
91 } catch (RepositoryException e) {
92 throw new ArgeoException("Cannot proxy " + path, e);
93 }
94 }
95
96 protected synchronized Node retrieveAndSave(String path) {
97 try {
98 Node node = retrieve(jcrAdminSession, path);
99 if (node == null)
100 return null;
101 jcrAdminSession.save();
102 return node;
103 } catch (RepositoryException e) {
104 JcrUtils.discardQuietly(jcrAdminSession);
105 throw new ArgeoException("Cannot retrieve and save " + path, e);
106 } finally {
107 notifyAll();
108 }
109 }
110
111 /** Session is not saved */
112 protected synchronized Node proxyUrl(Session session, String remoteUrl,
113 String path) throws RepositoryException {
114 Node node = null;
115 if (session.itemExists(path)) {
116 // throw new ArgeoException("Node " + path + " already exists");
117 }
118 InputStream in = null;
119 try {
120 URL u = new URL(remoteUrl);
121 in = u.openStream();
122 node = importFile(session, path, in);
123 } catch (IOException e) {
124 if (log.isDebugEnabled()) {
125 log.debug("Cannot read " + remoteUrl + ", skipping... "
126 + e.getMessage());
127 // log.trace("Cannot read because of ", e);
128 }
129 JcrUtils.discardQuietly(session);
130 } finally {
131 IOUtils.closeQuietly(in);
132 }
133 return node;
134 }
135
136 protected synchronized Node importFile(Session session, String path,
137 InputStream in) throws RepositoryException {
138 Binary binary = null;
139 try {
140 Node content = null;
141 Node node = null;
142 if (!session.itemExists(path)) {
143 node = JcrUtils.mkdirs(session, path, NodeType.NT_FILE,
144 NodeType.NT_FOLDER, false);
145 content = node.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
146 } else {
147 node = session.getNode(path);
148 content = node.getNode(Node.JCR_CONTENT);
149 }
150 binary = session.getValueFactory().createBinary(in);
151 content.setProperty(Property.JCR_DATA, binary);
152 JcrUtils.updateLastModifiedAndParents(node, null);
153 return node;
154 } finally {
155 JcrUtils.closeQuietly(binary);
156 }
157 }
158
159 /** Whether the file should be updated. */
160 protected Boolean shouldUpdate(Session clientSession, String nodePath) {
161 return false;
162 }
163
164 public void setJcrRepository(Repository jcrRepository) {
165 this.jcrRepository = jcrRepository;
166 }
167
168 public void setProxyWorkspace(String localWorkspace) {
169 this.proxyWorkspace = localWorkspace;
170 }
171
172 }