Merge branch 'master' of ../xml

This commit is contained in:
Robert von Burg 2014-09-16 09:04:39 +02:00
commit 242c5a4264
36 changed files with 2013 additions and 0 deletions

4
li.strolch.persistence.xml/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
target/
.project
.settings
.classpath

View File

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed 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.

View File

@ -0,0 +1,6 @@
li.strolch.persistence.xml
==========================
[![Build Status](http://jenkins.eitchnet.ch/buildStatus/icon?job=li.strolch.persistence.xml)](http://jenkins.eitchnet.ch/view/strolch/job/li.strolch.persistence.xml/)
XML Persistence Implementation for Strolch

View File

@ -0,0 +1,94 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>li.strolch</groupId>
<artifactId>li.strolch.parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../li.strolch.parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>li.strolch.persistence.xml</artifactId>
<name>li.strolch.persistence.xml</name>
<description>Reference Persistence Implementation for Strolch</description>
<url>https://github.com/eitchnet/li.strolch.persistence.xml</url>
<inceptionYear>2011</inceptionYear>
<issueManagement>
<system>Github Issues</system>
<url>https://github.com/eitchnet/li.strolch.persistence.xml/issues</url>
</issueManagement>
<scm>
<connection>scm:git:https://github.com/eitchnet/li.strolch.persistence.xml.git</connection>
<developerConnection>scm:git:git@github.com:eitch/li.strolch.persistence.xml.git</developerConnection>
<url>https://github.com/eitchnet/li.strolch.persistence.xml</url>
</scm>
<dependencies>
<!-- main -->
<dependency>
<groupId>li.strolch</groupId>
<artifactId>li.strolch.model</artifactId>
</dependency>
<dependency>
<groupId>li.strolch</groupId>
<artifactId>li.strolch.agent</artifactId>
</dependency>
<dependency>
<groupId>ch.eitchnet</groupId>
<artifactId>ch.eitchnet.xmlpers</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>li.strolch</groupId>
<artifactId>li.strolch.testbase</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/componentVersion.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,165 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import li.strolch.model.StrolchElement;
import li.strolch.persistence.api.StrolchDao;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.xmlpers.api.PersistenceTransaction;
import ch.eitchnet.xmlpers.objref.IdOfSubTypeRef;
import ch.eitchnet.xmlpers.objref.SubTypeRef;
import ch.eitchnet.xmlpers.objref.TypeRef;
public abstract class AbstractDao<T extends StrolchElement> implements StrolchDao<T> {
protected PersistenceTransaction tx;
protected AbstractDao(StrolchTransaction tx) {
XmlStrolchTransaction strolchTx = (XmlStrolchTransaction) tx;
this.tx = strolchTx.getTx();
}
protected abstract String getClassType();
protected IdOfSubTypeRef getIdRef(String type, String id) {
IdOfSubTypeRef idRef = this.tx.getObjectRefCache().getIdOfSubTypeRef(getClassType(), type, id);
return idRef;
}
protected SubTypeRef getTypeRef(String type) {
SubTypeRef typeRef = this.tx.getObjectRefCache().getSubTypeRef(getClassType(), type);
return typeRef;
}
@Override
public boolean hasElement(String type, String id) {
IdOfSubTypeRef ref = getIdRef(type, id);
return this.tx.getObjectDao().hasElement(ref);
}
@Override
public long querySize() {
long size = 0;
Set<String> types = queryTypes();
for (String type : types) {
SubTypeRef subTypeRef = getTypeRef(type);
size += this.tx.getMetadataDao().querySize(subTypeRef);
}
return size;
}
@Override
public long querySize(String type) {
SubTypeRef subTypeRef = getTypeRef(type);
return this.tx.getMetadataDao().querySize(subTypeRef);
}
@Override
public Set<String> queryKeySet() {
Set<String> keys = new HashSet<>();
Set<String> types = queryTypes();
for (String type : types) {
keys.addAll(queryKeySet(type));
}
return keys;
}
@Override
public Set<String> queryKeySet(String type) {
SubTypeRef typeRef = getTypeRef(type);
Set<String> keys = this.tx.getMetadataDao().queryKeySet(typeRef);
return keys;
}
@Override
public Set<String> queryTypes() {
TypeRef typeRef = this.tx.getObjectRefCache().getTypeRef(getClassType());
Set<String> types = this.tx.getMetadataDao().queryTypeSet(typeRef);
return types;
}
@Override
public T queryBy(String type, String id) {
T t = this.tx.getObjectDao().queryById(getIdRef(type, id));
return t;
}
@Override
public List<T> queryAll() {
List<T> objects = new ArrayList<>();
Set<String> types = queryTypes();
for (String type : types) {
List<T> objectsByType = this.tx.getObjectDao().queryAll(getTypeRef(type));
objects.addAll(objectsByType);
}
return objects;
}
@Override
public List<T> queryAll(String type) {
List<T> objectsByType = this.tx.getObjectDao().queryAll(getTypeRef(type));
return objectsByType;
}
@Override
public void save(T object) {
this.tx.getObjectDao().add(object);
}
@Override
public void saveAll(List<T> objects) {
this.tx.getObjectDao().addAll(objects);
}
@Override
public void update(T object) {
this.tx.getObjectDao().update(object);
}
@Override
public void updateAll(List<T> objects) {
this.tx.getObjectDao().updateAll(objects);
}
@Override
public void remove(T object) {
this.tx.getObjectDao().remove(object);
}
@Override
public void removeAll(List<T> objects) {
this.tx.getObjectDao().removeAll(objects);
}
@Override
public long removeAll() {
TypeRef typeRef = this.tx.getObjectRefCache().getTypeRef(getClassType());
return this.tx.getObjectDao().removeAllBy(typeRef);
}
@Override
public long removeAllBy(String type) {
SubTypeRef typeRef = getTypeRef(type);
return this.tx.getObjectDao().removeAllBy(typeRef);
}
}

View File

@ -0,0 +1,197 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import li.strolch.model.Tags;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.collections.DateRange;
import ch.eitchnet.xmlpers.api.PersistenceContext;
import ch.eitchnet.xmlpers.api.PersistenceTransaction;
import ch.eitchnet.xmlpers.objref.IdOfSubTypeRef;
import ch.eitchnet.xmlpers.objref.ObjectRef;
import ch.eitchnet.xmlpers.objref.SubTypeRef;
import ch.eitchnet.xmlpers.objref.TypeRef;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class XmlAuditDao implements AuditDao {
private PersistenceTransaction tx;
public XmlAuditDao(StrolchTransaction tx) {
XmlStrolchTransaction strolchTx = (XmlStrolchTransaction) tx;
this.tx = strolchTx.getTx();
}
protected String getClassType() {
return Tags.AUDIT;
}
protected IdOfSubTypeRef getIdRef(String type, Long id) {
IdOfSubTypeRef idRef = this.tx.getObjectRefCache().getIdOfSubTypeRef(getClassType(), type, id.toString());
return idRef;
}
protected SubTypeRef getTypeRef(String type) {
SubTypeRef typeRef = this.tx.getObjectRefCache().getSubTypeRef(getClassType(), type);
return typeRef;
}
@Override
public boolean hasElement(String type, Long id) {
IdOfSubTypeRef ref = getIdRef(type, id);
return this.tx.getObjectDao().hasElement(ref);
}
@Override
public long querySize(DateRange dateRange) {
long size = 0;
Set<String> types = queryTypes();
for (String type : types) {
// SubTypeRef subTypeRef = getTypeRef(type);
// size += this.tx.getMetadataDao().querySize(subTypeRef);
size += querySize(type, dateRange);
}
return size;
}
@Override
public long querySize(String type, DateRange dateRange) {
long size = 0;
// TODO re-think this nonsense... this might have a huge performance penalty
SubTypeRef subTypeRef = getTypeRef(type);
Set<String> keySet = this.tx.getMetadataDao().queryKeySet(subTypeRef);
for (String key : keySet) {
ObjectRef objectRef = subTypeRef.getChildIdRef(this.tx, key);
Audit audit = this.tx.getObjectDao().queryById(objectRef);
if (dateRange.contains(audit.getDate()))
size++;
}
// return this.tx.getMetadataDao().querySize(subTypeRef);
return size;
}
@Override
public Set<String> queryTypes() {
TypeRef typeRef = this.tx.getObjectRefCache().getTypeRef(getClassType());
Set<String> types = this.tx.getMetadataDao().queryTypeSet(typeRef);
return types;
}
@Override
public Audit queryBy(String type, Long id) {
Audit t = this.tx.getObjectDao().queryById(getIdRef(type, id));
return t;
}
@Override
public List<Audit> queryAll(String type, DateRange dateRange) {
List<Audit> audits = new ArrayList<>();
SubTypeRef subTypeRef = getTypeRef(type);
Set<String> keySet = this.tx.getMetadataDao().queryKeySet(subTypeRef);
for (String key : keySet) {
ObjectRef objectRef = subTypeRef.getChildIdRef(this.tx, key);
Audit audit = this.tx.getObjectDao().queryById(objectRef);
if (dateRange.contains(audit.getDate()))
audits.add(audit);
}
// this.tx.getObjectDao().queryAll(getTypeRef(type));
return audits;
}
@Override
public void save(Audit audit) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(audit);
this.tx.getFileDao().performCreate(ctx);
}
@Override
public void saveAll(List<Audit> audits) {
for (Audit audit : audits) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(audit);
this.tx.getFileDao().performCreate(ctx);
}
}
@Override
public void update(Audit audit) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(audit);
this.tx.getFileDao().performUpdate(ctx);
}
@Override
public void updateAll(List<Audit> audits) {
for (Audit audit : audits) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(audit);
this.tx.getFileDao().performUpdate(ctx);
}
}
@Override
public void remove(Audit audit) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(audit);
this.tx.getFileDao().performDelete(ctx);
}
@Override
public void removeAll(List<Audit> audits) {
for (Audit audit : audits) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(audit);
this.tx.getFileDao().performDelete(ctx);
}
}
@Override
public long removeAll(String type, DateRange dateRange) {
long removed = 0L;
SubTypeRef subTypeRef = getTypeRef(type);
Set<String> keySet = this.tx.getMetadataDao().queryKeySet(subTypeRef);
for (String key : keySet) {
ObjectRef objectRef = subTypeRef.getChildIdRef(this.tx, key);
Audit audit = this.tx.getObjectDao().queryById(objectRef);
if (dateRange.contains(audit.getDate())) {
PersistenceContext<Audit> ctx = this.tx.getObjectDao().createCtx(objectRef);
this.tx.getFileDao().performDelete(ctx);
removed++;
}
}
return removed;
}
@Override
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.Tags;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.StrolchTransaction;
public class XmlOrderDao extends AbstractDao<Order> implements OrderDao {
protected XmlOrderDao(StrolchTransaction tx) {
super(tx);
}
@Override
protected String getClassType() {
return Tags.ORDER;
}
@Override
public <U> List<U> doQuery(OrderQuery query, OrderVisitor<U> orderVisitor) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml;
import java.io.File;
import java.util.Properties;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.Tags;
import li.strolch.model.audit.Audit;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
import li.strolch.persistence.api.ResourceDao;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.xml.model.AuditContextFactory;
import li.strolch.persistence.xml.model.OrderContextFactory;
import li.strolch.persistence.xml.model.ResourceContextFactory;
import li.strolch.runtime.configuration.ComponentConfiguration;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.xmlpers.api.IoMode;
import ch.eitchnet.xmlpers.api.PersistenceConstants;
import ch.eitchnet.xmlpers.api.PersistenceManager;
import ch.eitchnet.xmlpers.api.PersistenceManagerLoader;
import ch.eitchnet.xmlpers.api.PersistenceTransaction;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class XmlPersistenceHandler extends StrolchComponent implements PersistenceHandler {
public static final String DB_STORE_PATH = "dbStore/"; //$NON-NLS-1$
private PersistenceManager persistenceManager;
public XmlPersistenceHandler(ComponentContainer container, String componentName) {
super(container, componentName);
}
@Override
public void initialize(ComponentConfiguration componentConfiguration) {
File basePathF = componentConfiguration.getRuntimeConfiguration().getRootPath();
File dbStorePathF = new File(basePathF, DB_STORE_PATH);
Properties properties = new Properties();
properties.setProperty(PersistenceConstants.PROP_VERBOSE, "true"); //$NON-NLS-1$
properties.setProperty(PersistenceConstants.PROP_XML_IO_MOD, IoMode.DOM.name());
properties.setProperty(PersistenceConstants.PROP_BASEPATH, dbStorePathF.getAbsolutePath());
this.persistenceManager = PersistenceManagerLoader.load(properties);
this.persistenceManager.getCtxFactory().registerPersistenceContextFactory(Resource.class, Tags.RESOURCE,
new ResourceContextFactory());
this.persistenceManager.getCtxFactory().registerPersistenceContextFactory(Order.class, Tags.ORDER,
new OrderContextFactory());
this.persistenceManager.getCtxFactory().registerPersistenceContextFactory(Audit.class, Tags.AUDIT,
new AuditContextFactory());
super.initialize(componentConfiguration);
}
@SuppressWarnings("resource")
@Override
public StrolchTransaction openTx(StrolchRealm realm, Certificate certificate, String action) {
PersistenceTransaction tx = this.persistenceManager.openTx(realm.getRealm());
return new XmlStrolchTransaction(getContainer().getPrivilegeHandler(), realm, certificate, action, tx, this);
}
@Override
public OrderDao getOrderDao(StrolchTransaction tx) {
return new XmlOrderDao(tx);
}
@Override
public ResourceDao getResourceDao(StrolchTransaction tx) {
return new XmlResourceDao(tx);
}
@Override
public AuditDao getAuditDao(StrolchTransaction tx) {
return new XmlAuditDao(tx);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml;
import java.util.List;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.Tags;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.ResourceDao;
import li.strolch.persistence.api.StrolchTransaction;
public class XmlResourceDao extends AbstractDao<Resource> implements ResourceDao {
protected XmlResourceDao(StrolchTransaction tx) {
super(tx);
}
@Override
protected String getClassType() {
return Tags.RESOURCE;
}
@Override
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}
}

View File

@ -0,0 +1,73 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml;
import java.util.Set;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.persistence.api.AbstractTransaction;
import li.strolch.persistence.api.PersistenceHandler;
import li.strolch.runtime.privilege.PrivilegeHandler;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.xmlpers.api.ModificationResult;
import ch.eitchnet.xmlpers.api.PersistenceTransaction;
import ch.eitchnet.xmlpers.api.TransactionResult;
public class XmlStrolchTransaction extends AbstractTransaction {
private XmlPersistenceHandler persistenceHandler;
private PersistenceTransaction tx;
public XmlStrolchTransaction(PrivilegeHandler privilegeHandler, StrolchRealm realm, Certificate certificate,
String action, PersistenceTransaction tx, XmlPersistenceHandler persistenceHandler) {
super(privilegeHandler, realm, certificate, action);
this.persistenceHandler = persistenceHandler;
this.tx = tx;
}
PersistenceTransaction getTx() {
return this.tx;
}
@Override
protected void writeChanges(li.strolch.persistence.api.TransactionResult txResult) throws Exception {
TransactionResult result = new TransactionResult();
this.tx.setTransactionResult(result);
this.tx.autoCloseableCommit();
Set<String> keys = result.getKeys();
for (String key : keys) {
ModificationResult modificationResult = result.getModificationResult(key);
txResult.incCreated(modificationResult.getCreated().size());
txResult.incUpdated(modificationResult.getUpdated().size());
txResult.incDeleted(modificationResult.getDeleted().size());
}
}
@Override
protected void rollback(li.strolch.persistence.api.TransactionResult txResult) throws Exception {
this.tx.autoCloseableRollback();
}
@Override
protected void commit() throws Exception {
// no-op
}
@Override
public PersistenceHandler getPersistenceHandler() {
return this.persistenceHandler;
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Tags;
import li.strolch.model.audit.Audit;
import ch.eitchnet.xmlpers.api.PersistenceContext;
import ch.eitchnet.xmlpers.api.PersistenceContextFactory;
import ch.eitchnet.xmlpers.objref.IdOfSubTypeRef;
import ch.eitchnet.xmlpers.objref.ObjectRef;
import ch.eitchnet.xmlpers.objref.ObjectReferenceCache;
public class AuditContextFactory implements PersistenceContextFactory<Audit> {
@Override
public PersistenceContext<Audit> createCtx(ObjectRef objectRef) {
PersistenceContext<Audit> ctx = new PersistenceContext<>(objectRef);
ctx.setParserFactory(new AuditParserFactory());
return ctx;
}
@Override
public PersistenceContext<Audit> createCtx(ObjectReferenceCache objectRefCache, Audit audit) {
IdOfSubTypeRef objectRef = objectRefCache.getIdOfSubTypeRef(Tags.AUDIT, audit.getElementType(), audit.getId()
.toString());
PersistenceContext<Audit> ctx = createCtx(objectRef);
ctx.setObject(audit);
return ctx;
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditFromDomReader;
import li.strolch.model.audit.AuditToDomVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.xmlpers.api.DomParser;
public class AuditDomParser implements DomParser<Audit> {
private Audit audit;
@Override
public Audit getObject() {
return this.audit;
}
@Override
public void setObject(Audit audit) {
this.audit = audit;
}
@Override
public Document toDom() {
AuditToDomVisitor auditDomVisitor = new AuditToDomVisitor();
return auditDomVisitor.visitAudit(this.audit);
}
@Override
public void fromDom(Document document) {
Element rootElement = document.getDocumentElement();
Audit audit = new AuditFromDomReader().from(rootElement);
this.audit = audit;
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.audit.Audit;
import ch.eitchnet.xmlpers.api.DomParser;
import ch.eitchnet.xmlpers.api.ParserFactory;
import ch.eitchnet.xmlpers.api.SaxParser;
public class AuditParserFactory implements ParserFactory<Audit> {
@Override
public DomParser<Audit> getDomParser() {
return new AuditDomParser();
}
@Override
public SaxParser<Audit> getSaxParser() {
throw new UnsupportedOperationException("Not yet implemented!"); //$NON-NLS-1$
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Order;
import li.strolch.model.Tags;
import ch.eitchnet.xmlpers.api.PersistenceContext;
import ch.eitchnet.xmlpers.api.PersistenceContextFactory;
import ch.eitchnet.xmlpers.objref.IdOfSubTypeRef;
import ch.eitchnet.xmlpers.objref.ObjectRef;
import ch.eitchnet.xmlpers.objref.ObjectReferenceCache;
public class OrderContextFactory implements PersistenceContextFactory<Order> {
@Override
public PersistenceContext<Order> createCtx(ObjectRef objectRef) {
PersistenceContext<Order> ctx = new PersistenceContext<>(objectRef);
ctx.setParserFactory(new OrderParserFactory());
return ctx;
}
@Override
public PersistenceContext<Order> createCtx(ObjectReferenceCache objectRefCache, Order t) {
IdOfSubTypeRef objectRef = objectRefCache.getIdOfSubTypeRef(Tags.ORDER, t.getType(), t.getId());
PersistenceContext<Order> ctx = createCtx(objectRef);
ctx.setObject(t);
return ctx;
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Order;
import li.strolch.model.xml.OrderToDomVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.xmlpers.api.DomParser;
public class OrderDomParser implements DomParser<Order> {
private Order order;
@Override
public Order getObject() {
return this.order;
}
@Override
public void setObject(Order object) {
this.order = object;
}
@Override
public Document toDom() {
OrderToDomVisitor orderDomVisitor = new OrderToDomVisitor();
orderDomVisitor.visit(this.order);
return orderDomVisitor.getDocument();
}
@Override
public void fromDom(Document document) {
Element rootElement = document.getDocumentElement();
Order order = new Order(rootElement);
this.order = order;
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Order;
import ch.eitchnet.xmlpers.api.DomParser;
import ch.eitchnet.xmlpers.api.ParserFactory;
import ch.eitchnet.xmlpers.api.SaxParser;
public class OrderParserFactory implements ParserFactory<Order> {
@Override
public DomParser<Order> getDomParser() {
return new OrderDomParser();
}
@Override
public SaxParser<Order> getSaxParser() {
throw new UnsupportedOperationException("Not yet implemented!"); //$NON-NLS-1$
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Resource;
import li.strolch.model.Tags;
import ch.eitchnet.xmlpers.api.PersistenceContext;
import ch.eitchnet.xmlpers.api.PersistenceContextFactory;
import ch.eitchnet.xmlpers.objref.IdOfSubTypeRef;
import ch.eitchnet.xmlpers.objref.ObjectRef;
import ch.eitchnet.xmlpers.objref.ObjectReferenceCache;
public class ResourceContextFactory implements PersistenceContextFactory<Resource> {
@Override
public PersistenceContext<Resource> createCtx(ObjectRef objectRef) {
PersistenceContext<Resource> ctx = new PersistenceContext<>(objectRef);
ctx.setParserFactory(new ResourceParserFactory());
return ctx;
}
@Override
public PersistenceContext<Resource> createCtx(ObjectReferenceCache objectRefCache, Resource t) {
IdOfSubTypeRef objectRef = objectRefCache.getIdOfSubTypeRef(Tags.RESOURCE, t.getType(), t.getId());
PersistenceContext<Resource> ctx = createCtx(objectRef);
ctx.setObject(t);
return ctx;
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Resource;
import li.strolch.model.xml.ResourceToDomVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.xmlpers.api.DomParser;
public class ResourceDomParser implements DomParser<Resource> {
private Resource resource;
@Override
public Resource getObject() {
return this.resource;
}
@Override
public void setObject(Resource resource) {
this.resource = resource;
}
@Override
public Document toDom() {
ResourceToDomVisitor domVisitor = new ResourceToDomVisitor();
domVisitor.visit(this.resource);
return domVisitor.getDocument();
}
@Override
public void fromDom(Document document) {
Element rootElement = document.getDocumentElement();
Resource resource = new Resource(rootElement);
this.resource = resource;
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.xml.model;
import li.strolch.model.Resource;
import ch.eitchnet.xmlpers.api.DomParser;
import ch.eitchnet.xmlpers.api.ParserFactory;
import ch.eitchnet.xmlpers.api.SaxParser;
public class ResourceParserFactory implements ParserFactory<Resource> {
@Override
public DomParser<Resource> getDomParser() {
return new ResourceDomParser();
}
@Override
public SaxParser<Resource> getSaxParser() {
throw new UnsupportedOperationException("Not yet implemented!"); //$NON-NLS-1$
}
}

View File

@ -0,0 +1,6 @@
groupId=${project.groupId}
artifactId=${project.artifactId}
artifactVersion=${project.version}
scmRevision=r${buildNumber}
scmBranch=${scmBranch}
buildTimestamp=${buildTimestamp}

View File

@ -0,0 +1,73 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.impl.dao.test;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants;
import li.strolch.runtime.privilege.PrivilegeHandler;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.eitchnet.privilege.model.Certificate;
public class ExistingDbTest {
private static final String TEST = "test"; //$NON-NLS-1$
public static final String RUNTIME_PATH = "target/existingDbRuntime/"; //$NON-NLS-1$
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
public static final String CONFIG_SRC = "src/test/resources/existingDbRuntime"; //$NON-NLS-1$
protected static RuntimeMock runtimeMock;
@BeforeClass
public static void beforeClass() {
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);
runtimeMock = new RuntimeMock();
runtimeMock.mockRuntime(rootPath, configSrc);
runtimeMock.startContainer();
}
@AfterClass
public static void afterClass() {
runtimeMock.destroyRuntime();
}
@Test
public void shouldQueryExistingData() {
PrivilegeHandler privilegeHandler = runtimeMock.getAgent().getContainer().getPrivilegeHandler();
Certificate certificate = privilegeHandler.authenticate(TEST, TEST.getBytes());
try (StrolchTransaction tx = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, TEST)) {
Resource resource = tx.getResourceMap().getBy(tx, "MyType", "@1"); //$NON-NLS-1$ //$NON-NLS-2$
assertNotNull("Should be able to read existing element from db", resource); //$NON-NLS-1$
Order order = tx.getOrderMap().getBy(tx, "MyType", "@1"); //$NON-NLS-1$//$NON-NLS-2$
assertNotNull("Should be able to read existing element from db", order); //$NON-NLS-1$
}
}
}

View File

@ -0,0 +1,136 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.impl.dao.test;
import static li.strolch.model.ModelGenerator.createOrder;
import static li.strolch.model.ModelGenerator.createResource;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import li.strolch.agent.api.Observer;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants;
import li.strolch.runtime.privilege.PrivilegeHandler;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.xmlpers.api.ModificationResult;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ObserverUpdateTest {
private static final String TEST = "test"; //$NON-NLS-1$
public static final String RUNTIME_PATH = "target/observerUpdateStrolchRuntime/"; //$NON-NLS-1$
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
public static final String CONFIG_SRC = "src/test/resources/cachedruntime"; //$NON-NLS-1$
protected static RuntimeMock runtimeMock;
protected RuntimeMock getRuntimeMock() {
return runtimeMock;
}
@BeforeClass
public static void beforeClass() {
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);
runtimeMock = new RuntimeMock();
runtimeMock.mockRuntime(rootPath, configSrc);
new File(rootPath, DB_STORE_PATH_DIR).mkdir();
runtimeMock.startContainer();
}
@AfterClass
public static void afterClass() {
runtimeMock.destroyRuntime();
}
public final class ElementAddedObserver implements Observer {
Map<String, ModificationResult> results = new HashMap<>();
private ModificationResult getModificationResult(String key) {
ModificationResult result = this.results.get(key);
if (result == null) {
result = new ModificationResult(key);
this.results.put(key, result);
}
return result;
}
@Override
public void update(String key, List<StrolchRootElement> elements) {
getModificationResult(key).getUpdated().addAll(elements);
}
@Override
public void remove(String key, List<StrolchRootElement> elements) {
getModificationResult(key).getDeleted().addAll(elements);
}
@Override
public void add(String key, List<StrolchRootElement> elements) {
getModificationResult(key).getCreated().addAll(elements);
}
}
@Test
public void shouldReceiveUpdates() {
// register an observer for orders and resources
ElementAddedObserver observer = new ElementAddedObserver();
StrolchRealm realm = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM);
realm.getObserverHandler().registerObserver(Tags.ORDER, observer);
realm.getObserverHandler().registerObserver(Tags.RESOURCE, observer);
PrivilegeHandler privilegeHandler = runtimeMock.getAgent().getContainer().getPrivilegeHandler();
Certificate certificate = privilegeHandler.authenticate(TEST, TEST.getBytes());
// create order
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = realm.openTx(certificate, TEST)) {
tx.getOrderMap().add(tx, newOrder);
}
// create resource
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = realm.openTx(certificate, TEST)) {
tx.getResourceMap().add(tx, newResource);
}
assertEquals(2, observer.results.size());
assertEquals(1, observer.results.get(Tags.ORDER).getCreated().size());
assertEquals(1, observer.results.get(Tags.RESOURCE).getCreated().size());
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.impl.dao.test;
import java.io.File;
import li.strolch.testbase.runtime.AbstractModelTest;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class XmlCachedDaoTest extends AbstractModelTest {
public static final String RUNTIME_PATH = "target/cachedStrolchRuntime/"; //$NON-NLS-1$
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
public static final String CONFIG_SRC = "src/test/resources/cachedruntime"; //$NON-NLS-1$
protected static RuntimeMock runtimeMock;
@Override
protected RuntimeMock getRuntimeMock() {
return runtimeMock;
}
@BeforeClass
public static void beforeClass() {
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);
runtimeMock = new RuntimeMock();
runtimeMock.mockRuntime(rootPath, configSrc);
new File(rootPath, DB_STORE_PATH_DIR).mkdir();
runtimeMock.startContainer();
}
@AfterClass
public static void afterClass() {
runtimeMock.destroyRuntime();
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed 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 li.strolch.persistence.impl.dao.test;
import java.io.File;
import li.strolch.testbase.runtime.AbstractModelTest;
import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class XmlTransactionalDaoTest extends AbstractModelTest {
public static final String RUNTIME_PATH = "target/transactionalStrolchRuntime/"; //$NON-NLS-1$
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
public static final String CONFIG_SRC = "src/test/resources/transactionalruntime"; //$NON-NLS-1$
protected static RuntimeMock runtimeMock;
@Override
protected RuntimeMock getRuntimeMock() {
return runtimeMock;
}
@BeforeClass
public static void beforeClass() {
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);
runtimeMock = new RuntimeMock();
runtimeMock.mockRuntime(rootPath, configSrc);
new File(rootPath, DB_STORE_PATH_DIR).mkdir();
runtimeMock.startContainer();
}
@AfterClass
public static void afterClass() {
runtimeMock.destroyRuntime();
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<Privilege>
<Container>
<Parameters>
<!-- parameters for the container itself -->
<Parameter name="autoPersistOnPasswordChange" value="true" />
</Parameters>
<EncryptionHandler class="ch.eitchnet.privilege.handler.DefaultEncryptionHandler">
<Parameters>
<Parameter name="hashAlgorithm" value="SHA-256" />
</Parameters>
</EncryptionHandler>
<PersistenceHandler class="ch.eitchnet.privilege.handler.XmlPersistenceHandler">
<Parameters>
<Parameter name="basePath" value="target/strolchRuntime/config" />
<Parameter name="modelXmlFile" value="PrivilegeModel.xml" />
</Parameters>
</PersistenceHandler>
</Container>
<Policies>
<Policy name="DefaultPrivilege" class="ch.eitchnet.privilege.policy.DefaultPrivilege" />
</Policies>
</Privilege>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<UsersAndRoles>
<Users>
<User userId="1" username="agent">
<State>SYSTEM</State>
<Roles>
<Role>agent</Role>
</Roles>
</User>
<User userId="2" username="test" password="9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08">
<Firstname>Application</Firstname>
<Lastname>Administrator</Lastname>
<State>ENABLED</State>
<Locale>en_GB</Locale>
<Roles>
<Role>PrivilegeAdmin</Role>
<Role>AppUser</Role>
</Roles>
</User>
</Users>
<Roles>
<Role name="PrivilegeAdmin" />
<Role name="agent">
<Privilege name="li.strolch.agent.impl.StartRealms" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
<Role name="AppUser">
<Privilege name="li.strolch.service.api.Service" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
<Privilege name="li.strolch.model.query.StrolchQuery" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
</Roles>
</UsersAndRoles>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchConfiguration>
<env id="dev">
<Runtime>
<applicationName>StrolchPersistenceTest</applicationName>
<Properties>
<verbose>true</verbose>
</Properties>
</Runtime>
<Component>
<name>PrivilegeHandler</name>
<api>li.strolch.runtime.privilege.PrivilegeHandler</api>
<impl>li.strolch.runtime.privilege.DefaultStrolchPrivilegeHandler</impl>
<Properties>
<privilegeConfigFile>PrivilegeConfig.xml</privilegeConfigFile>
</Properties>
</Component>
<Component>
<name>RealmHandler</name>
<api>li.strolch.agent.api.RealmHandler</api>
<impl>li.strolch.agent.impl.DefaultRealmHandler</impl>
<depends>PrivilegeHandler</depends>
<depends>PersistenceHandler</depends>
<Properties>
<dataStoreMode>CACHED</dataStoreMode>
<enableAuditTrail>true</enableAuditTrail>
<enableObserverUpdates>true</enableObserverUpdates>
</Properties>
</Component>
<Component>
<name>PersistenceHandler</name>
<api>li.strolch.persistence.api.PersistenceHandler</api>
<impl>li.strolch.persistence.xml.XmlPersistenceHandler</impl>
<Properties>
<verbose>true</verbose>
</Properties>
</Component>
</env>
</StrolchConfiguration>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<Privilege>
<Container>
<Parameters>
<!-- parameters for the container itself -->
<Parameter name="autoPersistOnPasswordChange" value="true" />
</Parameters>
<EncryptionHandler class="ch.eitchnet.privilege.handler.DefaultEncryptionHandler">
<Parameters>
<Parameter name="hashAlgorithm" value="SHA-256" />
</Parameters>
</EncryptionHandler>
<PersistenceHandler class="ch.eitchnet.privilege.handler.XmlPersistenceHandler">
<Parameters>
<Parameter name="basePath" value="target/strolchRuntime/config" />
<Parameter name="modelXmlFile" value="PrivilegeModel.xml" />
</Parameters>
</PersistenceHandler>
</Container>
<Policies>
<Policy name="DefaultPrivilege" class="ch.eitchnet.privilege.policy.DefaultPrivilege" />
</Policies>
</Privilege>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<UsersAndRoles>
<Users>
<User userId="1" username="agent">
<State>SYSTEM</State>
<Roles>
<Role>agent</Role>
</Roles>
</User>
<User userId="2" username="test" password="9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08">
<Firstname>Application</Firstname>
<Lastname>Administrator</Lastname>
<State>ENABLED</State>
<Locale>en_GB</Locale>
<Roles>
<Role>PrivilegeAdmin</Role>
<Role>AppUser</Role>
</Roles>
</User>
</Users>
<Roles>
<Role name="PrivilegeAdmin" />
<Role name="agent">
<Privilege name="li.strolch.agent.impl.StartRealms" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
<Role name="AppUser">
<Privilege name="li.strolch.service.api.Service" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
<Privilege name="li.strolch.model.query.StrolchQuery" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
</Roles>
</UsersAndRoles>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchConfiguration>
<env id="dev">
<Runtime>
<applicationName>StrolchPersistenceTest</applicationName>
<Properties>
<verbose>true</verbose>
</Properties>
</Runtime>
<Component>
<name>PrivilegeHandler</name>
<api>li.strolch.runtime.privilege.PrivilegeHandler</api>
<impl>li.strolch.runtime.privilege.DefaultStrolchPrivilegeHandler</impl>
<Properties>
<privilegeConfigFile>PrivilegeConfig.xml</privilegeConfigFile>
</Properties>
</Component>
<Component>
<name>RealmHandler</name>
<api>li.strolch.agent.api.RealmHandler</api>
<impl>li.strolch.agent.impl.DefaultRealmHandler</impl>
<depends>PrivilegeHandler</depends>
<depends>PersistenceHandler</depends>
<Properties>
<dataStoreMode>CACHED</dataStoreMode>
</Properties>
</Component>
<Component>
<name>PersistenceHandler</name>
<api>li.strolch.persistence.api.PersistenceHandler</api>
<impl>li.strolch.persistence.xml.XmlPersistenceHandler</impl>
<Properties>
<verbose>true</verbose>
</Properties>
</Component>
</env>
</StrolchConfiguration>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Order Date="2014-01-10T22:13:50.826+01:00" Id="@1" Name="My Order 0" State="CREATED" Type="MyType">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World"/>
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00"/>
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch"/>
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566"/>
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77"/>
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3"/>
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true"/>
</ParameterBag>
</Order>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Resource Id="@1" Name="My Resource 0" Type="MyType">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World"/>
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00"/>
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch"/>
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566"/>
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77"/>
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3"/>
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true"/>
</ParameterBag>
</Resource>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC
"-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p [%t] %C{1} %M - %m%n" />
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="sample.log"/>
<param name="BufferedIO" value="true" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p [%t] %C{1} %M - %m%n" />
</layout>
</appender>
<logger name="ch.eitchnet">
<level value="info" />
</logger>
<root>
<priority value="info" />
<appender-ref ref="CONSOLE" />
<!-- appender-ref ref="FILE" / -->
</root>
</log4j:configuration>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<Privilege>
<Container>
<Parameters>
<!-- parameters for the container itself -->
<Parameter name="autoPersistOnPasswordChange" value="true" />
</Parameters>
<EncryptionHandler class="ch.eitchnet.privilege.handler.DefaultEncryptionHandler">
<Parameters>
<Parameter name="hashAlgorithm" value="SHA-256" />
</Parameters>
</EncryptionHandler>
<PersistenceHandler class="ch.eitchnet.privilege.handler.XmlPersistenceHandler">
<Parameters>
<Parameter name="basePath" value="target/strolchRuntime/config" />
<Parameter name="modelXmlFile" value="PrivilegeModel.xml" />
</Parameters>
</PersistenceHandler>
</Container>
<Policies>
<Policy name="DefaultPrivilege" class="ch.eitchnet.privilege.policy.DefaultPrivilege" />
</Policies>
</Privilege>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<UsersAndRoles>
<Users>
<User userId="1" username="agent">
<State>SYSTEM</State>
<Roles>
<Role>agent</Role>
</Roles>
</User>
<User userId="2" username="test" password="9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08">
<Firstname>Application</Firstname>
<Lastname>Administrator</Lastname>
<State>ENABLED</State>
<Locale>en_GB</Locale>
<Roles>
<Role>PrivilegeAdmin</Role>
<Role>AppUser</Role>
</Roles>
</User>
</Users>
<Roles>
<Role name="PrivilegeAdmin" />
<Role name="agent">
<Privilege name="li.strolch.agent.impl.StartRealms" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
<Role name="AppUser">
<Privilege name="li.strolch.service.api.Service" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
<Privilege name="li.strolch.model.query.StrolchQuery" policy="DefaultPrivilege">
<AllAllowed>true</AllAllowed>
</Privilege>
</Role>
</Roles>
</UsersAndRoles>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchConfiguration>
<env id="dev">
<Runtime>
<applicationName>StrolchPersistenceTest</applicationName>
<Properties>
<verbose>true</verbose>
</Properties>
</Runtime>
<Component>
<name>PrivilegeHandler</name>
<api>li.strolch.runtime.privilege.PrivilegeHandler</api>
<impl>li.strolch.runtime.privilege.DefaultStrolchPrivilegeHandler</impl>
<Properties>
<privilegeConfigFile>PrivilegeConfig.xml</privilegeConfigFile>
</Properties>
</Component>
<Component>
<name>RealmHandler</name>
<api>li.strolch.agent.api.RealmHandler</api>
<impl>li.strolch.agent.impl.DefaultRealmHandler</impl>
<depends>PrivilegeHandler</depends>
<depends>PersistenceHandler</depends>
<Properties>
<dataStoreMode>TRANSACTIONAL</dataStoreMode>
<enableAuditTrail>true</enableAuditTrail>
</Properties>
</Component>
<Component>
<name>PersistenceHandler</name>
<api>li.strolch.persistence.api.PersistenceHandler</api>
<impl>li.strolch.persistence.xml.XmlPersistenceHandler</impl>
<Properties>
<verbose>true</verbose>
</Properties>
</Component>
</env>
</StrolchConfiguration>