added init script if repository is not yet initialized

This commit is contained in:
Lucio Lelii 2022-02-05 11:42:41 +01:00
parent 6d3e9394c4
commit 9d3bd619bd
11 changed files with 520 additions and 22 deletions

View File

@ -1,8 +1,11 @@
FROM luciolelii/smartgears:3.4.3-SNAPSHOT
FROM luciolelii/smartgears:latest
ARG REPOUSER=admin
ARG REPOPWD=admin
COPY ./target/storagehub.war /usr/local/tomcat/webapps/
COPY ./docker/jackrabbit /app/jackrabbit
COPY ./docker/storagehub.xml /usr/local/tomcat/conf/Catalina/localhost/
RUN unzip /usr/local/tomcat/webapps/storagehub.war -d /usr/local/tomcat/webapps/storagehub
RUN sed -i 's/<root /<logger name="org.gcube.data.access.storagehub" level="DEBUG" \/>\n <root /g' /usr/local/tomcat/lib/logback.xml
RUN sed -i 's/${{adminId}}/workspace/g; s/${{adminPwd}}/gcube/g' /usr/local/tomcat/webapps/storagehub/WEB-INF/web.xml
RUN sed -i "s/{{adminId}}/$REPOUSER/g; s/{{adminPwd}}/$REPOPWD/g" /usr/local/tomcat/webapps/storagehub/WEB-INF/web.xml

View File

@ -39,8 +39,7 @@
<SecurityManager class="org.apache.jackrabbit.core.DefaultSecurityManager" />
<AccessManager class="org.apache.jackrabbit.core.security.DefaultAccessManager" />
<LoginModule class="org.apache.jackrabbit.core.security.authentication.DefaultLoginModule">
<param name="adminId" value="workspace" />
<param name="adminPassword" value="gcube" />
<param name="adminId" value="admin" />
</LoginModule>
</Security>
<!-- location of workspaces root directory and name of default workspace -->

View File

@ -1,7 +1,6 @@
${{adminId}}=workspace
${{adminPwd}}=gcube
${{db-host}}=localhost
${{db-host}}=postgres
${{ws-db}}=workspace-db
${{dbUser}}=ws-db-user
${{dbPwd}}=dbPwd
${{dbPwd}}=dbPwd

View File

@ -2,34 +2,60 @@ package org.gcube.data.access.storagehub;
import javax.inject.Singleton;
import javax.jcr.Repository;
import javax.jcr.SimpleCredentials;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.jackrabbit.api.JackrabbitRepository;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.gcube.data.access.storagehub.services.RepositoryInitializer;
import org.gcube.data.access.storagehub.services.admin.InitScript;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class RepositoryInitializerImpl implements RepositoryInitializer{
private static Logger log = LoggerFactory.getLogger(RepositoryInitializerImpl.class);
private Repository repository;
private boolean jackrabbitInitialized = false;
@Override
public synchronized Repository getRepository(){
return repository;
}
protected RepositoryInitializerImpl() throws Exception{
InitialContext context = new InitialContext();
Context environment = (Context) context.lookup("java:comp/env");
repository = (Repository) environment.lookup("jcr/repository");
}
public void shutdown() {
((JackrabbitRepository)repository).shutdown();
}
@Override
public synchronized void initContainerAtFirstStart(SimpleCredentials credentials) {
try {
log.info("credential are {} {}",credentials.getUserID(), new String(credentials.getPassword()));
JackrabbitSession ses = (JackrabbitSession) repository.login(credentials);
try {
boolean notAlreadyDone = !jackrabbitInitialized && !ses.getRootNode().hasNode("Home");
if (notAlreadyDone)
new InitScript().init(ses);
else log.info("jackrabbit is already initialized");
}finally {
ses.logout();
}
} catch (Exception e) {
log.warn("error initialising Jackrabbit",e);
}
jackrabbitInitialized = true;
}
}

View File

@ -1,8 +1,13 @@
package org.gcube.data.access.storagehub;
import javax.jcr.SimpleCredentials;
import org.gcube.data.access.storagehub.handlers.CredentialHandler;
import org.gcube.data.access.storagehub.services.RepositoryInitializer;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -11,19 +16,34 @@ public class StorageHubAppllicationManager implements ApplicationManager {
private static Logger logger = LoggerFactory.getLogger(StorageHubAppllicationManager.class);
private boolean alreadyShutDown = false;
private boolean alreadyInit = false;
public static RepositoryInitializer repository;
@Override
public synchronized void onInit() {
logger.info("jackrabbit initialization started");
logger.info("initializing storagehub");
try {
repository = new RepositoryInitializerImpl();
} catch (Exception e) {
logger.error("ERROR INITIALIZING REPOSITORY",e);
ApplicationContext ctx = ContextProvider.get();
if (!alreadyInit) {
logger.info("jackrabbit initialization started");
try {
repository = new RepositoryInitializerImpl();
} catch (Exception e) {
logger.error("ERROR INITIALIZING REPOSITORY",e);
}
SimpleCredentials credentials = CredentialHandler.getAdminCredentials(ctx.application());
repository.initContainerAtFirstStart(credentials);
alreadyInit = true;
}
} catch (Throwable e) {
logger.error("unexpected error initiliazing storagehub",e);
}
repository.getRepository();
}
@Override

View File

@ -1,10 +1,13 @@
package org.gcube.data.access.storagehub.services;
import javax.jcr.Repository;
import javax.jcr.SimpleCredentials;
public interface RepositoryInitializer {
Repository getRepository();
void initContainerAtFirstStart(SimpleCredentials credentials);
void shutdown();
}

View File

@ -0,0 +1,60 @@
package org.gcube.data.access.storagehub.services.admin;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.JackrabbitWorkspace;
import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
import org.apache.jackrabbit.commons.cnd.CndImporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InitScript {
private static Logger log = LoggerFactory.getLogger(InitScript.class);
public void init(JackrabbitSession ses) throws Exception{
log.info("init started");
try {
initNodeTypes(ses);
ses.getRootNode().addNode("Home");
ses.getRootNode().addNode("Share");
PrivilegeManager pm = ((JackrabbitWorkspace) ses.getWorkspace()).getPrivilegeManager();
pm.registerPrivilege("hl:writeAll", false, new String[0]);
ses.save();
}catch (Exception e) {
log.error("init error", e);
throw e;
}
log.info("init finished");
}
void initNodeTypes(Session ses) throws Exception{
InputStream stream = InitScript.class.getResourceAsStream("/init/NodeType.cnd");
if (stream == null)
throw new Exception("NodeType.cnd inputStream is null");
InputStreamReader inputstream = new InputStreamReader(stream, Charset.forName("UTF-8"));
// Register the custom node types defined in the CND file, using JCR Commons CndImporter
log.info("start to register the custom node types defined in the CND file...");
NodeType[] nodeTypes = CndImporter.registerNodeTypes(inputstream, ses, true);
for (NodeType nt : nodeTypes) {
log.info("Registered: {} ", nt.getName());
}
log.info("custom node types registered");
}
}

View File

@ -112,7 +112,7 @@ public class ScriptManager {
if (AbstractScript.class.isAssignableFrom(clazz)) {
AbstractScript scriptInstance = (AbstractScript) clazz.newInstance();
RealRun realRun = new RealRun(ses, scriptInstance, login, parentId, name);
RealRun realRun = new RealRun(ses, scriptInstance, login, parentId, name, true);
if (asynch) {
new Thread(AuthorizedTasks.bind(realRun)).start();
}else realRun.run();
@ -138,14 +138,16 @@ public class ScriptManager {
String login;
String parentId;
String name;
public RealRun(JackrabbitSession ses, AbstractScript instance, String login, String parentId, String name) {
boolean writeResult = true;
public RealRun(JackrabbitSession ses, AbstractScript instance, String login, String parentId, String name, boolean writeResult) {
super();
this.ses = ses;
this.instance = instance;
this.login = login;
this.parentId = parentId;
this.name = name;
this.writeResult = writeResult;
}
@ -161,6 +163,7 @@ public class ScriptManager {
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
result+= "\n"+sw.toString();
log.warn("error executing script {}",instance.getClass().getSimpleName(), t);
}
try( InputStream stream = new ReaderInputStream(new StringReader(result))){

View File

@ -0,0 +1,385 @@
<mix = 'http://www.jcp.org/jcr/mix/1.0'>
<nt = 'http://www.jcp.org/jcr/nt/1.0'>
<nthl = 'http://ip-server:port/hl/nthl'>
<hl = 'http://ip-server:port/hl'>
[nthl:user] > nt:base
- hl:portalLogin (String)
- hl:uuid (String)
- hl:scope (String)
[nthl:applicationData] > mix:lastModified,mix:referenceable,nt:base
- hl:type (String) mandatory
- hl:data (binary) mandatory
[nthl:home] > nt:folder
- hl:scopes (String) multiple
- hl:version (Long)
[nthl:accountingEntry] > nt:base
- hl:user (String)
- hl:date (Date) mandatory
- hl:version (String)
[nthl:accountingEntryCreate] > nthl:accountingEntry
- hl:itemName (String)
[nthl:accountingEntryRead] > nthl:accountingEntry
- hl:itemName (String)
[nthl:accountingEntryEnabledPublicAccess] > nthl:accountingEntryRead
[nthl:accountingEntryDisabledPublicAccess] > nthl:accountingEntryRead
[nthl:accountingEntryPaste] > nthl:accountingEntry
- hl:fromPath (String) mandatory
[nthl:accountingEntryUpdate] > nthl:accountingEntry
- hl:itemName (String) mandatory
[nthl:accountingEntryShare] > nthl:accountingEntry
- hl:itemName (String) mandatory
- hl:members (String) multiple
[nthl:accountingEntryUnshare] > nthl:accountingEntry
- hl:itemName (String) mandatory
[nthl:accountingEntryRestore] > nthl:accountingEntry
- hl:itemName (String) mandatory
[nthl:accountingEntryDelete] > nthl:accountingEntry
- hl:itemName (String) mandatory
- hl:fromPath (String)
[nthl:accountingFolderEntryRenaming] > nthl:accountingEntry
- hl:oldItemName (String) mandatory
- hl:newItemName (String)
[nthl:accountingFolderEntryRemoval] > nthl:accountingEntry
- hl:itemType (String) mandatory
- hl:folderItemType (String)
- hl:itemName (String) mandatory
- hl:mimeType (String)
[nthl:accountingFolderEntryCut] > nthl:accountingFolderEntryRemoval
[nthl:accountingFolderEntryAdd] > nthl:accountingFolderEntryRemoval
[nthl:accountingEntryACL] > nthl:accountingEntry
- hl:itemName (String) mandatory
- hl:members (String) multiple
[nthl:accountingSet] > nt:base
+ * (nthl:accountingEntry)
[nthl:readersSet] > nt:base
+ * (nthl:accountingEntryRead)
[nthl:workspaceItem] > mix:referenceable, mix:title, mix:lastModified, nt:hierarchyNode, mix:lockable
- hl:portalLogin (String)
- hl:lastAction (String) mandatory
- hl:oldRemotePath (String)
- hl:storagePath (String)
- hl:moved (Boolean)
- hl:hidden (Boolean)
= 'false'
autocreated
- hl:IsSystemFolder (Boolean)
= 'false'
autocreated
- hl:isPublic (Boolean)
= 'false'
autocreated
+ hl:readers (nthl:readersSet)
= nthl:readersSet
autocreated
+ hl:accounting (nthl:accountingSet)
= nthl:accountingSet
autocreated
+ hl:metadata (nt:unstructured)
= nt:unstructured
autocreated
+ hl:owner(nthl:user)
= nthl:user
autocreated
+ hl:payloadBackend (nthl:payloadBackend)
= nthl:payloadBackend
mandatory autocreated
+ *
[nthl:workspaceSharedItem] > nthl:workspaceItem, mix:shareable
- hl:privilege (String)
+ hl:members (nt:unstructured)
= nt:unstructured
autocreated
- hl:isVreFolder (Boolean)
- hl:displayName (String)
+ hl:users (nt:unstructured)
= nt:unstructured
autocreated
+ * (nthl:workspaceItem)
[nthl:workspaceVreItem] > nthl:workspaceSharedItem
- hl:groupId (String)
- hl:scope (String)
[nthl:workspaceReference] > nthl:workspaceItem
- hl:reference (Reference)
[nthl:workspaceLeafItem] > nthl:workspaceItem, nt:file
- hl:workspaceItemType (String)
- hl:workflowId (String)
- hl:workflowStatus (String)
- hl:workflowData (String)
[nthl:workspaceSmartItem] > nthl:workspaceLeafItem
[nthl:itemSentRequest] > mix:created, nt:base
+ hl:owner(nthl:user)
= nthl:user
mandatory autocreated
- hl:subject (String) mandatory
- hl:body (String) mandatory
- hl:read (Boolean) mandatory
- hl:open (Boolean) mandatory
- hl:addresses (String) mandatory multiple
+ hl:attachments (nt:folder)
= nt:folder
mandatory autocreated
[nthl:itemSentRequestSH] > nthl:itemSentRequest, mix:shareable, mix:created, nt:base
[nthl:rootItemSentRequest] > nt:folder
+ * (nthl:itemSentRequest)
= nthl:itemSentRequest
[nthl:folderBulkCreator] > nt:base
- hl:folderId (String) mandatory
- hl:status (Long)
= '0'
mandatory autocreated
- hl:failures (Long)
= '0'
mandatory autocreated
- hl:requests (Long) mandatory
[nthl:rootFolderBulkCreator] > nt:folder
+ * (nthl:folderBulkCreator)
= nthl:folderBulkCreator
[nthl:workspaceLeafItemContent] > nt:base
[nthl:payloadBackend] > nt:base
- hl:storageName (String)
+ hl:parameters (nt:unstructured)
= nt:unstructured
autocreated
[nthl:file] > nt:resource
- hl:size (long)
- hl:remotePath (String)
- hl:storageId (String)
- hl:storageName (String)
+ hl:payloadBackend (nthl:payloadBackend)
= nthl:payloadBackend
mandatory autocreated
[nthl:image] > nthl:file
- hl:width (Long)
= '0'
mandatory autocreated
- hl:height (Long)
= '0'
mandatory autocreated
- hl:thumbnailWidth (Long)
= '0'
mandatory autocreated
- hl:thumbnailHeight (Long)
= '0'
mandatory autocreated
- hl:thumbnailData (binary)
[nthl:pdf] > nthl:file
- hl:numberOfPages (long)
- hl:version (string)
- hl:author (string)
- hl:title (string)
- hl:producer (string)
[nthl:documentAlternativeLink] > nt:base
- hl:parentUri (String) mandatory
- hl:uri (String) mandatory
- hl:name (String) mandatory
- hl:mimeType (String) mandatory
[nthl:documentPartLink] > nthl:documentAlternativeLink
[nthl:documentItemContent] > nthl:workspaceLeafItemContent
- hl:collectionName (String) mandatory
- hl:oid (String) mandatory
+ hl:metadata (nt:unstructured)
= nt:unstructured
mandatory autocreated
+ hl:annotations (nt:unstructured)
= nt:unstructured
mandatory autocreated
+ hl:alternatives (nt:unstructured)
= nt:unstructured
mandatory autocreated
+ hl:parts (nt:unstructured)
= nt:unstructured
mandatory autocreated
[nthl:metadataItemContent] > nthl:workspaceLeafItemContent, nthl:file
- hl:schema (String) mandatory
- hl:language (String) mandatory
- hl:collectionName (String) mandatory
- hl:oid (String) mandatory
[nthl:annotationItemContet] > nthl:workspaceLeafItemContent
- hl:oid (String) mandatory
+ hl:annotations (nt:unstructured)
= nt:unstructured
mandatory autocreated
[nthl:queryItemContent] > nthl:workspaceLeafItemContent
- hl:query (String) mandatory
- hl:queryType (String) mandatory
[nthl:aquamapsItemContent] > nthl:workspaceLeafItemContent, nthl:file
- hl:mapName (String) mandatory
- hl:mapType (String) mandatory
- hl:author (String) mandatory
- hl:numberOfSpecies (Long) mandatory
- hl:boundingBox (String) mandatory
- hl:PSOThreshold (Double) mandatory
- hl:numberOfImages (Long) mandatory
+ hl:images(nt:unstructured)
= nt:unstructured
mandatory autocreated
[nthl:timeSeriesItemContent] > nthl:workspaceLeafItemContent, nthl:file
- hl:id (String) mandatory
- hl:title (String) mandatory
- hl:description (String) mandatory
- hl:creator (String) mandatory
- hl:created (String) mandatory
- hl:publisher (String) mandatory
- hl:sourceId (String) mandatory
- hl:sourceName (String) mandatory
- hl:rights (String) mandatory
- hl:dimension (Long) mandatory
- hl:headerLabels (String)
[nthl:reportItemContent] > nthl:workspaceLeafItemContent, nthl:file
- hl:created (Date) mandatory
- hl:lastEdit (Date) mandatory
- hl:author (String) mandatory
- hl:lastEditBy (String) mandatory
- hl:templateName (String) mandatory
- hl:numberOfSection (Long) mandatory
- hl:status (String) mandatory
[nthl:reportTemplateContent] > nthl:workspaceLeafItemContent, nthl:file
- hl:created (Date) mandatory
- hl:lastEdit (Date) mandatory
- hl:author (String) mandatory
- hl:lastEditBy (String) mandatory
- hl:numberOfSection (Long) mandatory
- hl:status (String) mandatory
[nthl:externalResourceLinkContent] > nthl:workspaceLeafItemContent
- hl:mimeType (String)
- hl:size (long) mandatory
- hl:resourceId (String) mandatory
- hl:servicePlugin (String) mandatory
[nthl:tabularDataLinkContent] > nthl:workspaceLeafItemContent
- hl:tableID (String) mandatory
- hl:tableTemplateID (String) mandatory
- hl:provenance (String) mandatory
- hl:runtimeResourceID (String) mandatory
- hl:operator (String)
[nthl:smartFolderContent] > nt:base
- hl:query (String) mandatory
- hl:folderId (String)
[nthl:externalFile] > nthl:workspaceLeafItem
[nthl:externalImage] > nthl:workspaceLeafItem
[nthl:externalPdf] > nthl:workspaceLeafItem
[nthl:externalUrl] > nthl:workspaceLeafItem
[nthl:query] > nthl:workspaceLeafItem
[nthl:aquamapsItem] > nthl:workspaceLeafItem
[nthl:timeSeriesItem] > nthl:workspaceLeafItem
[nthl:report] > nthl:workspaceLeafItem
[nthl:reportTemplate] > nthl:workspaceLeafItem
[nthl:workflowReport] > nthl:workspaceLeafItem
[nthl:workflowTemplate] > nthl:workspaceLeafItem
[nthl:gCubeMetadata] > nthl:workspaceLeafItem
[nthl:gCubeDocument] > nthl:workspaceLeafItem
[nthl:gCubeDocumentLink] > nthl:workspaceLeafItem
[nthl:gCubeImageDocumentLink] > nthl:workspaceLeafItem
[nthl:gCubePDFDocumentLink] > nthl:workspaceLeafItem
[nthl:gCubeImageDocument] > nthl:workspaceLeafItem
[nthl:gCubePDFDocument] > nthl:workspaceLeafItem
[nthl:gCubeURLDocument] > nthl:workspaceLeafItem
[nthl:gCubeAnnotation] > nthl:workspaceLeafItem
[nthl:externalResourceLink] > nthl:workspaceLeafItem
[nthl:tabularDataLink] > nthl:workspaceLeafItem
[nthl:gCubeItem] > nthl:workspaceItem
- hl:scopes (String) mandatory multiple
- hl:creator (String) mandatory
- hl:itemType (String) mandatory
- hl:properties (String)
- hl:isShared (Boolean)
- hl:sharedRootId (String)
+ hl:property (nt:unstructured)
= nt:unstructured
autocreated
[nthl:trashItem] > nthl:workspaceItem
- hl:name (String)
- hl:deletedBy (String)
- hl:originalParentId (String)
- hl:deletedFrom (String)
- hl:deletedTime (Date)
- hl:mimeType (String)
- hl:length (String)
- hl:isFolder (Boolean)
+ * (nthl:workspaceItem)

View File

@ -25,7 +25,7 @@ The projects leading to this software have received funding from a series of
Version
--------------------------------------------------
1.5.0-SNAPSHOT (20220201-153509)
1.5.0-SNAPSHOT (20220205-103540)
Please see the file named "changelog.xml" in this directory for the release notes.

View File

@ -2,11 +2,11 @@
<web-app>
<context-param>
<param-name>admin-username</param-name>
<param-value>${{adminId}}</param-value>
<param-value>{{adminId}}</param-value>
</context-param>
<context-param>
<param-name>admin-pwd</param-name>
<param-value>${{adminPwd}}</param-value>
<param-value>{{adminPwd}}</param-value>
</context-param>
<context-param>
<param-name>resolver-basepath</param-name>