]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - proxy/AbstractUrlProxy.java
1 package org
.argeo
.jcr
.proxy
;
3 import java
.io
.InputStream
;
6 import javax
.jcr
.Binary
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.nodetype
.NodeType
;
14 import org
.apache
.commons
.io
.IOUtils
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.ArgeoException
;
18 import org
.argeo
.jcr
.JcrUtils
;
20 /** Base class for URL based proxys. */
21 public abstract class AbstractUrlProxy
implements ResourceProxy
{
22 private final static Log log
= LogFactory
.getLog(AbstractUrlProxy
.class);
24 private Repository jcrRepository
;
25 private Session jcrAdminSession
;
27 protected abstract String
retrieve(String relativePath
);
31 jcrAdminSession
= jcrRepository
.login();
32 beforeInitSessionSave();
33 if (jcrAdminSession
.hasPendingChanges())
34 jcrAdminSession
.save();
35 } catch (RepositoryException e
) {
36 JcrUtils
.discardQuietly(jcrAdminSession
);
37 throw new ArgeoException("Cannot initialize Maven proxy", e
);
42 * Called before the (admin) session is saved at the end of the
43 * initialization. Does nothing by default, to be overridden.
45 protected void beforeInitSessionSave() throws RepositoryException
{
49 JcrUtils
.logoutQuietly(jcrAdminSession
);
53 * Called before the (admin) session is logged out when resources are
54 * released. Does nothing by default, to be overridden.
56 protected void beforeDestroySessionLogout() throws RepositoryException
{
59 public Node
proxy(Session jcrSession
, String path
) {
62 String nodePath
= getNodePath(path
);
63 if (!jcrSession
.itemExists(nodePath
)) {
64 String nodeIdentifier
= retrieve(path
);
65 if (nodeIdentifier
== null) {
66 // log.warn("Could not proxy " + path);
69 node
= jcrSession
.getNodeByIdentifier(nodeIdentifier
);
72 node
= jcrSession
.getNode(nodePath
);
74 } catch (RepositoryException e
) {
75 throw new ArgeoException("Cannot proxy " + path
, e
);
80 protected Node
proxyUrl(String baseUrl
, String path
) {
82 String remoteUrl
= baseUrl
+ path
;
83 if (log
.isTraceEnabled())
84 log
.trace("baseUrl=" + remoteUrl
);
85 InputStream in
= null;
87 URL u
= new URL(remoteUrl
);
89 node
= importFile(getNodePath(path
), in
);
90 if (log
.isDebugEnabled())
91 log
.debug("Imported " + remoteUrl
+ " to " + node
);
92 } catch (Exception e
) {
93 if (log
.isTraceEnabled())
94 log
.trace("Cannot read " + remoteUrl
+ ", skipping... "
96 if (log
.isTraceEnabled()) {
97 log
.trace("Cannot read because of ", e
);
100 IOUtils
.closeQuietly(in
);
106 protected synchronized Node
importFile(String nodePath
, InputStream in
) {
107 // FIXME allow parallel proxying
108 Binary binary
= null;
110 Node node
= JcrUtils
.mkdirs(jcrAdminSession
, nodePath
,
111 NodeType
.NT_FILE
, NodeType
.NT_FOLDER
, false);
112 Node content
= node
.addNode(Node
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
113 binary
= jcrAdminSession
.getValueFactory().createBinary(in
);
114 content
.setProperty(Property
.JCR_DATA
, binary
);
115 jcrAdminSession
.save();
117 } catch (RepositoryException e
) {
118 JcrUtils
.discardQuietly(jcrAdminSession
);
119 throw new ArgeoException("Cannot initialize Maven proxy", e
);
121 JcrUtils
.closeQuietly(binary
);
125 protected Session
getJcrAdminSession() {
126 return jcrAdminSession
;
129 public void setJcrRepository(Repository jcrRepository
) {
130 this.jcrRepository
= jcrRepository
;