blob: 7ac51f46ddc2f3d09020019bab48cf2fea26e77f [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.discovery.base.its.setup.mock;
import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Value;
import javax.servlet.http.HttpServletRequest;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.testing.jcr.RepositoryProvider;
import org.apache.sling.commons.testing.jcr.RepositoryUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.LoggerFactory;
public class MockedResourceResolver implements ResourceResolver {
private final SlingRepository repository;
private volatile boolean closed = false;
private Session session;
private List<MockedResource> resources = new LinkedList<MockedResource>();
private final ArtificialDelay delay;
public MockedResourceResolver() throws RepositoryException {
this(null, null);
}
public MockedResourceResolver(SlingRepository repositoryOrNull) throws RepositoryException {
this(repositoryOrNull, null);
}
public MockedResourceResolver(SlingRepository repositoryOrNull, ArtificialDelay delay) throws RepositoryException {
this.delay = delay;
if (repositoryOrNull==null) {
this.repository = RepositoryProvider.instance().getRepository();
Session adminSession = null;
try {
adminSession = this.repository.loginAdministrative(null);
RepositoryUtil.registerSlingNodeTypes(adminSession);
} catch ( final IOException ioe ) {
throw new RepositoryException(ioe);
} finally {
if ( adminSession != null ) {
adminSession.logout();
}
}
} else {
this.repository = repositoryOrNull;
}
}
public Session getSession() throws RepositoryException {
synchronized (this) {
if (session != null) {
return session;
}
if (closed) {
throw new IllegalStateException("ResourceResolver closed");
}
session = createSession();
return session;
}
}
public Repository getRepository() {
return repository;
}
private Session createSession() throws RepositoryException {
final Credentials credentials = new SimpleCredentials("admin",
"admin".toCharArray());
return repository.login(credentials, "default");
}
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type.equals(Session.class)) {
try {
return (AdapterType) getSession();
} catch (RepositoryException e) {
throw new RuntimeException("RepositoryException: " + e, e);
}
} else if (type.equals(Repository.class)) {
return (AdapterType) getRepository();
}
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Resource resolve(HttpServletRequest request, String absPath) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Resource resolve(String absPath) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
@Deprecated
public Resource resolve(HttpServletRequest request) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public String map(String resourcePath) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public String map(HttpServletRequest request, String resourcePath) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Resource getResource(String path) {
Session session;
try {
session = getSession();
session.getNode(path);
} catch (PathNotFoundException e) {
return null;
} catch (RepositoryException e) {
throw new RuntimeException("RepositoryException: " + e, e);
}
return new MockedResource(this, path, "nt:unstructured");
}
@Override
public Resource getResource(Resource base, String path) {
if (base.getPath().equals("/")) {
return getResource("/" + path);
} else {
return getResource(base.getPath() + "/" + path);
}
}
@Override
public String[] getSearchPath() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Iterator<Resource> listChildren(Resource parent) {
try {
Node node = parent.adaptTo(Node.class);
final NodeIterator nodes = node.getNodes();
return new Iterator<Resource>() {
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public Resource next() {
Node next = nodes.nextNode();
try {
return new MockedResource(MockedResourceResolver.this,
next.getPath(), "nt:unstructured");
} catch (RepositoryException e) {
throw new RuntimeException("RepositoryException: " + e,
e);
}
}
@Override
public boolean hasNext() {
return nodes.hasNext();
}
};
} catch (RepositoryException e) {
throw new RuntimeException("RepositoryException: " + e, e);
}
}
@Override
public Iterable<Resource> getChildren(Resource parent) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Iterator<Resource> findResources(String query, String language) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Iterator<Map<String, Object>> queryResources(String query,
String language) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public ResourceResolver clone(Map<String, Object> authenticationInfo)
throws LoginException {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public boolean isLive() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void close() {
Iterator<MockedResource> it = resources.iterator();
while (it.hasNext()) {
MockedResource r = it.next();
r.close();
}
if (session != null) {
if (session.isLive()) {
session.logout();
}
session = null;
}
closed = true;
}
public void register(MockedResource mockedResource) {
resources.add(mockedResource);
}
@Override
public String getUserID() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Iterator<String> getAttributeNames() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Object getAttribute(String name) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void delete(Resource resource) throws PersistenceException {
if (resources.contains(resource)) {
resources.remove(resource);
Node node = resource.adaptTo(Node.class);
try {
node.remove();
} catch (RepositoryException e) {
throw new PersistenceException("RepositoryException: "+e, e);
}
} else {
throw new UnsupportedOperationException("Not implemented");
}
}
@Override
public Resource create(Resource parent, String name,
Map<String, Object> properties) throws PersistenceException {
final Node parentNode = parent.adaptTo(Node.class);
boolean success = false;
try {
final Node child;
if (properties!=null && properties.containsKey("jcr:primaryType")) {
child = parentNode.addNode(name, (String) properties.get("jcr:primaryType"));
} else {
child = parentNode.addNode(name);
}
if (properties!=null) {
final Iterator<Entry<String, Object>> it = properties.entrySet().iterator();
while(it.hasNext()) {
final Entry<String, Object> entry = it.next();
if (entry.getKey().equals("jcr:primaryType")) {
continue;
}
if (entry.getValue() instanceof String) {
child.setProperty(entry.getKey(), (String)entry.getValue());
} else if (entry.getValue() instanceof Boolean) {
child.setProperty(entry.getKey(), (Boolean)entry.getValue());
} else if (entry.getValue() instanceof Calendar) {
child.setProperty(entry.getKey(), (Calendar)entry.getValue());
} else if (entry.getValue() == null) {
child.setProperty(entry.getKey(), (Value)null);
} else {
throw new UnsupportedOperationException("Not implemented (entry.getValue(): "+entry.getValue()+")");
}
}
}
Resource result = getResource(parent, name);
success = true;
return result;
} catch (RepositoryException e) {
throw new RuntimeException(e);
} finally {
LoggerFactory.getLogger(this.getClass()).info("create: creating of "+name+" under "+parent+" was successful="+success);
}
}
@Override
public void revert() {
try {
this.session.refresh(false);
} catch (final RepositoryException re) {
throw new RuntimeException("Unable to commit changes.", re);
}
}
@Override
public void commit() throws PersistenceException {
if (delay!=null) {
delay.delay("pre.commit");
}
try {
this.session.save();
} catch (final RepositoryException re) {
throw new PersistenceException("Unable to commit changes.", re);
}
}
@Override
public boolean hasChanges() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public String getParentResourceType(Resource resource) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getParentResourceType(String resourceType) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isResourceType(Resource resource, String resourceType) {
// TODO Auto-generated method stub
return false;
}
@Override
public void refresh() {
// TODO Auto-generated method stub
}
@Override
public boolean hasChildren(Resource arg0) {
// TODO Auto-generated method stub
return this.getChildren(arg0).iterator().hasNext();
}
}