Skip to content

Commit

Permalink
Adding some logging and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 11, 2013
1 parent df5b5ca commit 25724b4
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 114 deletions.
Expand Up @@ -31,7 +31,6 @@
import javax.jcr.Session;

import org.fcrepo.kernel.rdf.RdfContext;
import org.fcrepo.kernel.utils.NamespaceTools;
import org.slf4j.Logger;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -59,7 +58,7 @@ public class NamespaceContext extends RdfContext {
public NamespaceContext(final Session session) throws RepositoryException {
super();
final NamespaceRegistry namespaceRegistry =
NamespaceTools.getNamespaceRegistry(session);
session.getWorkspace().getNamespaceRegistry();
assert (namespaceRegistry != null) : new RepositoryException(
"Couldn't find namespace registry in repository!");
final ImmutableMap.Builder<String, String> namespaces =
Expand All @@ -74,10 +73,12 @@ public NamespaceContext(final Session session) throws RepositoryException {
LOGGER.debug("Discovered namespace prefix \"{}\" with URI \"{}\"",
prefix, nsURI);
final String rdfNsUri = getRDFNamespaceForJcrNamespace(nsURI);
// first, let's put the namespace in context
namespaces.put(prefix, rdfNsUri);
LOGGER.debug("Added namespace prefix \"{}\" with URI \"{}\"",
prefix, rdfNsUri);
final Node nsSubject = createURI(rdfNsUri);
// now, some triples describing this namespace
nsTriples.add(create(nsSubject, type.asNode(), VOAF_VOCABULARY
.asNode()));
nsTriples.add(create(nsSubject, HAS_NAMESPACE_PREFIX.asNode(),
Expand Down
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static javax.jcr.PropertyType.BINARY;
import static javax.jcr.query.Query.JCR_SQL2;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_DATASTREAM;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_OBJECT;
Expand All @@ -30,6 +31,7 @@

import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;

import javax.jcr.Binary;
import javax.jcr.Node;
Expand Down Expand Up @@ -60,6 +62,7 @@

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;

/**
* Convenience class with static methods for manipulating Fedora types in the
Expand Down Expand Up @@ -128,50 +131,91 @@ public boolean apply(final Node node) {
* Translates a {@link NodeType} to its {@link String} name.
*/
public static Function<NodeType, String> nodetype2name =
new Function<NodeType, String>() {
new Function<NodeType, String>() {

@Override
public String apply(final NodeType t) {
checkArgument(t != null, "null has no name!");
return t.getName();
}
};
@Override
public String apply(final NodeType t) {
checkArgument(t != null, "null has no name!");
return t.getName();
}
};

/**
* Translates a JCR {@link Value} to its {@link String} expression.
*/
public static Function<Value, String> value2string =
new Function<Value, String>() {

@Override
public String apply(final Value v) {
try {
checkArgument(v != null, "null has no appropriate "
+ "String representation!");
return v.getString();
} catch (final RepositoryException e) {
throw propagate(e);
new Function<Value, String>() {

@Override
public String apply(final Value v) {
try {
checkArgument(v != null, "null has no appropriate "
+ "String representation!");
return v.getString();
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};

/**
* Constructs an {@link Iterator} of {@link Value}s from any
* {@link Property}, multi-valued or not.
*/
public static Function<Property, Iterator<Value>> property2values =
new Function<Property, Iterator<Value>>() {

@Override
public Iterator<Value> apply(final Property p) {
try {
if (p.isMultiple()) {
LOGGER.debug("Found multi-valued property: {}", p);
return Iterators.forArray(p.getValues());
} else {
LOGGER.debug("Found single-valued property: {}", p);
return Iterators.forArray(new Value[] {p.getValue()});
}
} catch (final RepositoryException e) {
throw propagate(e);
}
};
}
};

/**
* Check if a JCR property is a multivalued property or not
*/
public static Predicate<Property> isMultipleValuedProperty =
new Predicate<Property>() {

@Override
public boolean apply(final Property p) {
checkArgument(p != null,
"null is neither multiple or not multiple!");
try {
return p.isMultiple();
} catch (final RepositoryException e) {
throw propagate(e);
}
new Predicate<Property>() {

@Override
public boolean apply(final Property p) {
checkArgument(p != null,
"null is neither multiple nor not multiple!");
try {
return p.isMultiple();
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};

/**
* Check if a JCR property is a binary property or not
*/
public static Predicate<Property> isBinaryProperty =
new Predicate<Property>() {

@Override
public boolean apply(final Property p) {
checkArgument(p != null,
"null is neither binary nor not binary!");
try {
return p.getType() == BINARY;
} catch (final RepositoryException e) {
throw propagate(e);
}
};
}
};

/**
* Check if a node is "internal" and should not be exposed e.g. via the REST
Expand All @@ -185,8 +229,8 @@ public boolean apply(final Node n) {
"null is neither multiple or not multiple!");
try {
final NodeType primaryNodeType = n.getPrimaryNodeType();
return primaryNodeType != null &&
primaryNodeType.isNodeType("mode:system");
return primaryNodeType != null
&& primaryNodeType.isNodeType("mode:system");
} catch (final RepositoryException e) {
throw propagate(e);
}
Expand All @@ -197,48 +241,47 @@ public boolean apply(final Node n) {
* Retrieves a JCR {@link ValueFactory} for use with a @ link Node}
*/
public static Function<Node, ValueFactory> getValueFactory =
new Function<Node, ValueFactory>() {

@Override
public ValueFactory apply(final Node n) {
try {
checkArgument(n != null,
"null has no ValueFactory associated with it!");
return n.getSession().getValueFactory();
} catch (final RepositoryException e) {
throw propagate(e);
}
new Function<Node, ValueFactory>() {

@Override
public ValueFactory apply(final Node n) {
try {
checkArgument(n != null,
"null has no ValueFactory associated with it!");
return n.getSession().getValueFactory();
} catch (final RepositoryException e) {
throw propagate(e);
}
};
}
};

/**
* Map a JCR property to an RDF property with the right namespace URI and
* local name
*/
public static Function<Property, com.hp.hpl.jena.rdf.model.Property> getPredicateForProperty =
new Function<Property, com.hp.hpl.jena.rdf.model.Property>() {

@Override
public com.hp.hpl.jena.rdf.model.Property apply(
final Property property) {
LOGGER.trace("Creating predicate for property: {}",
property);
try {
if (property instanceof Namespaced) {
final Namespaced nsProperty = (Namespaced) property;
final String uri = nsProperty.getNamespaceURI();
return createProperty(
getRDFNamespaceForJcrNamespace(uri),
nsProperty.getLocalName());
} else {
return createProperty(property.getName());
}
} catch (final RepositoryException e) {
throw propagate(e);
new Function<Property, com.hp.hpl.jena.rdf.model.Property>() {

@Override
public com.hp.hpl.jena.rdf.model.Property apply(
final Property property) {
LOGGER.trace("Creating predicate for property: {}", property);
try {
if (property instanceof Namespaced) {
final Namespaced nsProperty = (Namespaced) property;
final String uri = nsProperty.getNamespaceURI();
return createProperty(
getRDFNamespaceForJcrNamespace(uri), nsProperty
.getLocalName());
} else {
return createProperty(property.getName());
}

} catch (final RepositoryException e) {
throw propagate(e);
}
};

}
};

/**
* ISODateTimeFormat is thread-safe and immutable, and the formatters it
Expand Down Expand Up @@ -280,7 +323,7 @@ public static Binary getBinary(final Node n, final InputStream i,
checkArgument(i != null,
"null cannot have a Binary created from it!");
final JcrValueFactory jcrValueFactory =
((JcrValueFactory) n.getSession().getValueFactory());
((JcrValueFactory) n.getSession().getValueFactory());
return jcrValueFactory.createBinary(i, hint);
} catch (final RepositoryException e) {
throw propagate(e);
Expand All @@ -294,8 +337,8 @@ public static Binary getBinary(final Node n, final InputStream i,
* @return
* @throws RepositoryException
*/
public static NodeTypeManager getNodeTypeManager(final Node node)
throws RepositoryException {
public static NodeTypeManager
getNodeTypeManager(final Node node) throws RepositoryException {
return node.getSession().getWorkspace().getNodeTypeManager();
}

Expand All @@ -308,12 +351,12 @@ public static NodeTypeManager getNodeTypeManager(final Node node)
* @return a JCR PropertyDefinition, if available, or null
* @throws javax.jcr.RepositoryException
*/
public static PropertyDefinition getDefinitionForPropertyName(
final Node node, final String propertyName)
throws RepositoryException {
public static PropertyDefinition
getDefinitionForPropertyName(final Node node,
final String propertyName) throws RepositoryException {
final PropertyDefinition[] propertyDefinitions =
getNodeTypeManager(node).getNodeType(FEDORA_RESOURCE)
.getPropertyDefinitions();
getNodeTypeManager(node).getNodeType(FEDORA_RESOURCE)
.getPropertyDefinitions();

for (final PropertyDefinition p : propertyDefinitions) {
if (p.getName().equals(propertyName)) {
Expand Down Expand Up @@ -353,8 +396,8 @@ public static String convertDateToXSDString(final long date) {
* @return
* @throws RepositoryException
*/
public static Version getBaseVersion(final Node node)
throws RepositoryException {
public static Version
getBaseVersion(final Node node) throws RepositoryException {
return node.getSession().getWorkspace().getVersionManager()
.getBaseVersion(node.getPath());
}
Expand All @@ -366,8 +409,8 @@ public static Version getBaseVersion(final Node node)
* @return
* @throws RepositoryException
*/
public static VersionHistory getVersionHistory(final Node node)
throws RepositoryException {
public static VersionHistory
getVersionHistory(final Node node) throws RepositoryException {
return getVersionHistory(node.getSession(), node.getPath());
}

Expand All @@ -389,19 +432,21 @@ public static VersionHistory getVersionHistory(final Session session,
* @return a double of the size of the fedora:datastream binary content
* @throws RepositoryException
*/
public static long getRepositoryCount(final Repository repository)
throws RepositoryException {
public static
long
getRepositoryCount(final Repository repository)
throws RepositoryException {
final Session session = repository.login();
try {
final QueryManager queryManager =
session.getWorkspace().getQueryManager();
session.getWorkspace().getQueryManager();

final String querystring =
"SELECT [" + JcrConstants.JCR_PATH + "] FROM [" +
FedoraJcrTypes.FEDORA_OBJECT + "]";
"SELECT [" + JcrConstants.JCR_PATH + "] FROM ["
+ FedoraJcrTypes.FEDORA_OBJECT + "]";

final QueryResult queryResults =
queryManager.createQuery(querystring, JCR_SQL2).execute();
queryManager.createQuery(querystring, JCR_SQL2).execute();

return queryResults.getRows().getSize();
} finally {
Expand All @@ -414,23 +459,25 @@ public static long getRepositoryCount(final Repository repository)
* @return a double of the size of the fedora:datastream binary content
* @throws RepositoryException
*/
public static long getRepositorySize(final Repository repository)
throws RepositoryException {
public static
long
getRepositorySize(final Repository repository)
throws RepositoryException {
final Session session = repository.login();
long sum = 0;
final QueryManager queryManager =
session.getWorkspace().getQueryManager();
session.getWorkspace().getQueryManager();

final String querystring =
"SELECT [" + FedoraJcrTypes.CONTENT_SIZE + "] FROM [" +
FedoraJcrTypes.FEDORA_BINARY + "]";
"SELECT [" + FedoraJcrTypes.CONTENT_SIZE + "] FROM ["
+ FedoraJcrTypes.FEDORA_BINARY + "]";

final QueryResult queryResults =
queryManager.createQuery(querystring, JCR_SQL2).execute();
queryManager.createQuery(querystring, JCR_SQL2).execute();

for (final RowIterator rows = queryResults.getRows(); rows.hasNext();) {
final Value value =
rows.nextRow().getValue(FedoraJcrTypes.CONTENT_SIZE);
rows.nextRow().getValue(FedoraJcrTypes.CONTENT_SIZE);
sum += value.getLong();
}

Expand Down

0 comments on commit 25724b4

Please sign in to comment.