Connect database with MyBatis without xml file - java

I am trying to connect a small java application with a database using MyBatis.
XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#xxxx:xxxx:xxxx"/>
<property name="username" value="xxxxx"/>
<property name="password" value="xxxxx"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="xml/Mapper.xml"/>
</mappers>
</configuration>
Before, I got the session as follows
String resource = "Configuration.xml";
SqlSession session = null;
try{
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
session = sqlMapper.openSession();
I want to connect to the database without the use of xml file. Any help will be appreciated.

Have you checked MyBatis 3 user guide? There is a section called "Building SqlSessionFactory
without XML". Also there is no need to use XML based mapping, you could use annotation based statement mapping and avoid XML configuration altogether.

This worked for me:
import javax.sql.DataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
String user = "xxxxx";
String password = "xxxxx";
String databasenameURL = "jdbc:oracle:thin:#xxxx:xxxx:xxxx";
String dbDriver = "oracle.jdbc.driver.OracleDriver";
DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
dbDriver, databasenameURL, user, password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development",
transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(configuration);

Maybe its late, but for future readers.
you can parse the xml to string and modify any value you want later.:
this worked for me:
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CharSequenceReader;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Reader;
public class MySqlSessionFactory
{
private static Logger log = Logger.getLogger(MySqlSessionFactory.class);
private static SqlSessionFactory sessionFactory;
public static SqlSessionFactory getSqlSessionFactory()
{
return sessionFactory;
}
/**
* Initialize SqlSessionFactory instance, to be used later in all the project
*
* #return boolean if sqlSessionFactory is build
*/
public static boolean initializeMySqlSessionFactory() throws IOException
{
log.trace("Enter method initializeMySqlSessionFactory.");
String resource = "mybatis-config.xml";
boolean result;
Reader reader = null;
Reader parsedReader = null;
try
{
reader = Resources.getResourceAsReader(resource);
String parsedXMLConfig = IOUtils.toString(reader);
parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${jdbcUrl}", "databaseURL");
parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${username}", "databaseUser");
parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${password}", "databasePassword");
parsedReader = new CharSequenceReader(parsedXMLConfig);
sessionFactory = new SqlSessionFactoryBuilder().build(parsedReader);
result = true;
}
catch(IOException e)
{
result = false;
log.error("Error calling initializeMySqlSessionFactory.", e);
throw new IOException(e);
}
finally
{
if(reader != null )
{
reader.close();
}
if(parsedReader != null )
{
parsedReader.close();
}
}
log.trace("Exit method initializeMySqlSessionFactory. Method result: " + result);
return result;
}
}
also with this way, you can change any specified field you want... and pay attention to put the same placeholder in the xml file (${jdbcUrl}, ${username}, ${password})

Related

Tomcat Multiple Datasources with decrypt password logic

I am facing some issues with while working with multiple datasources in Tomcat environment. Please find the details below.
I have below 2 datasources in my tomcat/conf/server.xml file
Data Source1:
<Resource name="myds1"
global="myds1"
auth="Container"
type="javax.sql.DataSource"
factory="com.tomcat.datasorceEncrypt.EncryptedDataSourceFactory"
driverClassName="oracle.jdbc.driver.OracleDriver"
singleton = "false"/>
Data Source2:
<Resource name="myds2"
global="myds2"
auth="Container"
type="javax.sql.DataSource"
factory="com.tomcat.datasorceEncrypt.EncryptedDataSourceFactory"
driverClassName="com.ibm.db2.jcc.DB2Driver"
singleton = "false"/>
Here is my EncryptedDataSourceFactory file which extends DataSourceFactory:
package com.tomcat.datasorceEncrypt;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;
import java.util.stream.Stream;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.sql.DataSource;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSourceFactory;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
import org.apache.tomcat.jdbc.pool.XADataSource;
public class EncryptedDataSourceFactory extends DataSourceFactory {
private static final Log log = LogFactory.getLog(EncryptedDataSourceFactory.class);
private static final String PROP_DIALECT = "dialect";
private static final String[] CUSTOM_PROPERTIES = new String[]{PROP_DIALECT};
private static final String[] PROPERTIES = Stream.of(ALL_PROPERTIES, CUSTOM_PROPERTIES).flatMap(Stream::of).toArray(String[]::new);
#Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
throws Exception {
if (obj != null && obj instanceof Reference) {
Reference ref = (Reference) obj;
Properties properties = new Properties();
for (int i = 0; i < PROPERTIES.length; ++i) {
String propertyName = PROPERTIES[i];
RefAddr ra = ref.get(propertyName);
if (ra != null) {
String propertyValue = ra.getContent().toString();
properties.setProperty(propertyName, propertyValue);
}
}
return this.createDataSource(properties, nameCtx,false);
} else {
return null;
}
}
#Override
public DataSource createDataSource(Properties properties, Context context, boolean XA) throws Exception {
// Here we decrypt our password.
PoolConfiguration poolProperties = parsePoolProperties(properties);
Properties dbProperties = loadProperties();
poolProperties.setPassword(CryptoUtility.decryptDBPass(dbProperties.getProperty("DB_key"), dbProperties.getProperty("DB_password")));
poolProperties.setUsername(dbProperties.getProperty("DB_username"));
poolProperties.setUrl(dbProperties.getProperty("DB_url"));
System.out.println(poolProperties.getPoolName() + "****-------*****" + poolProperties.getName() );
System.out.println(poolProperties.getDataSourceJNDI() + "****------***" + poolProperties.getDataSource());
// The rest of the code is copied from Tomcat's DataSourceFactory.
if (poolProperties.getDataSourceJNDI() != null && poolProperties.getDataSource() == null) {
performJNDILookup(context, poolProperties);
}
org.apache.tomcat.jdbc.pool.DataSource dataSource = XA ? new XADataSource(poolProperties)
: new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
dataSource.createPool();
return dataSource;
}
private Properties loadProperties() {
Properties prop = new Properties();
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("DBPassword.properties");
prop.load(inputStream);
}catch (Exception e) {
log.fatal("Error Loading the properties.", e);
throw new RuntimeException(e);
}
return prop;
}
}
I was trying to identify the datasource JNDI name from poolProperties.getDataSourceJNDI() so that I can apply proper credentials through my properties, but I am receiving poolProperties.getDataSourceJNDI() as null.
Have I missed any property in the Resource while creating datasource?
Note: While working with resource I could able to set user name and password though I have got poolProperties.getDataSourceJNDI() is null.
For timebeeing I have created one more class by extending DatasourceFactory for second datasource, it's working but I don't think it's an Idle solution.
In the routine, getObjectInstance, the parameter name can be used for this.
Based on the name, the decrypt method can be called for different data sources.
I had 8 datasources defined. I used this approach so that I just use one DataSourcefactory class.

Connecting to PostgreSQL-server with Hibernate from IntelliJ

I am attempting to persist data to a PostgreSQL-database via Hibernate, have put in my user/pass, checked that it's working, made a db and some tables.
When I compile, I get an error
org.postgresql.util.PSQLException: The server
requested password-based authentication, but no password was provided.
I'm using IntelliJ Ultimate 2017.1 and have tried using both the supplied pg driver and 42.00 as external library.
I've had it working in previous versions, but never seen this one before. Must admit I'm not very good at this.
Basically, my defined password does not get correctly passed on to the server. It seems like it recognized my username. I've temporarily evaded this problem by modifying my pg_hba.conf file to trust local connections without password, but I am going to persist the data on an online server, so I'm gonna need a better fix.
The driver has a standard URL template that looks like this:
jdbc:postgresql:{database::postgres}[\?<&,user={user:param},password={password:param},{:identifier}={:param}>]
Here is my hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:postgresql://localhost:5432/gigahertz</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<mapping class="no.hvl.dat101.gigahertz.ReservationJPA"/>
<!-- <property name="connection.username"/> -->
<!-- <property name="connection.password"/> -->
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
Here is my generated Main-class
package no.hvl.dat101.gigahertz;
import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import javax.persistence.metamodel.EntityType;
import java.util.Map;
/**
*/
public class Main {
private static final SessionFactory ourSessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
System.out.println("querying all the managed entities...");
final Metamodel metamodel = session.getSessionFactory().getMetamodel();
for (EntityType<?> entityType : metamodel.getEntities()) {
final String entityName = entityType.getName();
final Query query = session.createQuery("from " + entityName);
System.out.println("executing: " + query.getQueryString());
for (Object o : query.list()) {
System.out.println(" " + o);
}
}
} finally {
session.close();
}
}
}
Any advice appreciated!
The solution to this was to add the parameters in the try block in main in the form configuration.setProperty("hibernate.connection.username","u‌​sername) etc.
For some reason, the login details were not passed correctly by IntelliJ to the server.

How to add a document to the Alfresco Repository with Java code?

EDIT: Apparently I had to make an AMP and map it to Alfresco.war. But now I can't access the code I wrote, so I guess I'll have to use Webscripts and the like. Can someone provide an example of how to add a document to the Alfresco Repository with a Java backed webscript?
ORIGINAL QUESTION:
I've searched google-wide for a way to add a document to the Alfresco Repository with Java code. But I was not able to find a way that would work. I know how I can add a document to the Repository: use the NodeService. But the problem is that I cannot get an instance of the NodeService. I've tried to inject it with #Autowired, I've tried using a bean and I've tried using an ApplicationContext. None of the ways worked...
Way #1:
Injection in a class:
#Autowired
NodeService nodeService
Way #2:
In service-context.xml:
<bean id="somerandombeanname" class="management.FileManager" >
<property name="moduleId" value="${project.artifactId}" />
<property name="serviceRegistry" ref="ServiceRegistry" />
<property name="nodeService" ref="NodeService" />
<property name="transactionService" ref="TransactionService" />
<property name="contentService" ref="ContentService" />
</bean>
In the class I added a getter and setter for all the services and the serviceRegistry:
private NodeService nodeService;
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
Way #3:
appContext = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
serviceRegistry = (ServiceRegistry) appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
nodeService = serviceRegistry.getNodeService();
Ways #1 and #2 gave me a NullPointerException simply stating the NodeService is null. Way #3 gave a mile long StackTrace because of an AlfrescoRuntimeException because it failed to initialize a keystore:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssl.keyStore' defined in class path resource [alfresco/encryption-context.xml]: Invocation of init method failed; nested exception is org.alfresco.error.AlfrescoRuntimeException: 04180000 Failed to initialize keystore:
Location: E:/Alfresco/alf_data/keystore/ssl.keystore
Provider: null
Type: JCEKS
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at management.FileManager.<init>(FileManager.java:36)
at simple.start.main(start.java:25)
... 5 more
Caused by: org.alfresco.error.AlfrescoRuntimeException: 04180000 Failed to initialize keystore:
Location: E:/Alfresco/alf_data/keystore/ssl.keystore
Provider: null
Type: JCEKS
at org.alfresco.encryption.AlfrescoKeyStoreImpl.loadKeyStore(AlfrescoKeyStoreImpl.java:566)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.safeInit(AlfrescoKeyStoreImpl.java:537)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.init(AlfrescoKeyStoreImpl.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1639)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1580)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1510)
... 18 more
Caused by: java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(URLClassPath.java:494)
at sun.misc.URLClassPath.findResource(URLClassPath.java:176)
at java.net.URLClassLoader$2.run(URLClassLoader.java:551)
at java.net.URLClassLoader$2.run(URLClassLoader.java:549)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:548)
at java.lang.ClassLoader.getResource(ClassLoader.java:1147)
at org.springframework.core.io.ClassPathResource.resolveURL(ClassPathResource.java:147)
at org.springframework.core.io.ClassPathResource.exists(ClassPathResource.java:135)
at org.alfresco.encryption.SpringKeyResourceLoader.getSafeInputStream(SpringKeyResourceLoader.java:67)
at org.alfresco.encryption.SpringKeyResourceLoader.loadKeyMetaData(SpringKeyResourceLoader.java:133)
at org.alfresco.encryption.AlfrescoKeyStoreImpl$KeyInfoManager.loadKeyMetaData(AlfrescoKeyStoreImpl.java:1016)
at org.alfresco.encryption.AlfrescoKeyStoreImpl$KeyInfoManager.<init>(AlfrescoKeyStoreImpl.java:998)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.getKeyInfoManager(AlfrescoKeyStoreImpl.java:395)
at org.alfresco.encryption.AlfrescoKeyStoreImpl.loadKeyStore(AlfrescoKeyStoreImpl.java:560)
... 27 more
Yes, the keystore exists and yes I have regenerated a new keystore.
I'm using Alfresco 5.0.1 and I'm working on the Repository side (not Share).
#Autowired
NodeService nodeService
Will not work in alfresco
You need to inject it with proper setter method.
Your bean should be like below.
<bean id="somerandombeanname" class="management.FileManager" >
<property name="moduleId" value="${project.artifactId}" />
<property name="serviceRegistry" ref="ServiceRegistry" />
<property name="nodeService" ref="NodeService" />
<property name="transactionService" ref="TransactionService" />
<property name="contentService" ref="ContentService" />
</bean>
Your java class should contain following for injecting nodeService.
private NodeService nodeService;
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
Luckily I have code for file upload thorugh JAVA backed webscript. Hope this help you too.To create java backed webscript see this
Create one class named CustomFileUpload.java and put following content
package com.upload;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class CustomFileUpload extends DeclarativeWebScript {
private final String UPLOAD_FILE_PATH = "C:\\Users\\Test\\Desktop\\test.txt";
private int statusCode;
protected Map<String, Object> executeImpl(WebScriptRequest arg0, Status status, Cache cache) {
Map<String, Object> model = new HashMap<String, Object>();
try {
String URL = "http://localhost:8080/alfresco/service/upload/fileupload?alf_ticket=" +getAlfticket();
File file = new File(UPLOAD_FILE_PATH);
String filetype = "text/plain";
String filename = file.getName();
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(URL);
Part[] parts = {
new FilePart("filedata", filename, file, filetype, null),
new StringPart("filename", filename),
new StringPart("description", "This is test description"),
new StringPart("destination", "workspace://SpacesStore/bb424b1d-0418-4954-8591-b8c807264df0")
};
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
statusCode = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
if (statusCode == 200) {
model.put("result", "File uploaded successfully.");
return model;
} else {
model.put("result", "There was an error while uploading document.");
return model;
}
}
private static String getAlfticket() throws IOException, JSONException {
URL url = new URL("http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin&format=json");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String json = IOUtils.toString(in, encoding);
JSONObject getData = new JSONObject(json);
return getData.getJSONObject("data").get("ticket").toString();
}
}
NOTE: In destination you can put nodeRef of folder in which you want to upload.
Than create bean in context file name it whatever you want say mycustom-context.xml and put it in ALFRESCO_HOME\tomcat\shared\classes\alfresco\extension and content
<bean id="webscript.com.upload.customupload.post" class="com.upload.CustomFileUpload" parent="webscript">
</bean>
Finally register this web script in alfresco by creating customupload.post.desc.xml. and put
<webscript>
<shortname>File Upload</shortname>
<description>Upload files to user home</description>
<url>/upload/fileupload?alf_ticket={ticket}</url>
<format default="json"/>
<authentication>user</authentication>
</webscript>
And last create view as we have declared JSON is default format so we need to create customupload.post.json.ftl
${result}
And put these both files in ALFRESCO_HOME\tomcat\shared\classes\alfresco\extension\templates\webscripts\com\upload
Now restart server and hit http://localhost:8080/alfresco/service/upload/fileupload and you will see file uploaded in folder(Whatever you have given). For Reference
Finally! This is the solution for adding a file to the Repository:
CustomFileUpload.java:
package org.example;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.io.FileUtils;
public class CustomFileUpload extends DeclarativeWebScript {
private final String UPLOAD_FILE_PATH = "{someRandomFile}";
private final String UPLOAD_DESTINATION = "workspace://SpacesStore/{someRandomNodeRef}";
protected ServiceRegistry serviceRegistry;
public ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
File file = new File(UPLOAD_FILE_PATH);
// NodeRef parent = getCompanyHome();
NodeRef parent = new NodeRef(UPLOAD_DESTINATION);
String name = "name of file in Repository " + System.currentTimeMillis();
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
props.put(ContentModel.PROP_NAME, name);
// use the node service to create a new node
NodeRef node = serviceRegistry.getNodeService().createNode(
parent,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name),
ContentModel.TYPE_CONTENT, props).getChildRef();
// Use the content service to set the content onto the newly created
// node
ContentWriter writer = serviceRegistry.getContentService().getWriter(node, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.setEncoding("UTF-8");
String text = "";
try {
text = FileUtils.readFileToString(file);
} catch (IOException e) {
e.printStackTrace();
}
writer.putContent(text);
Map<String, Object> model = new HashMap<String, Object>();
if (status.getCode() == Status.STATUS_OK) {
model.put("resultRepoWS", "File \"" + file.getName() + "\" uploaded successfully to the repository. Status: " + status.getCode());
return model;
} else {
model.put("resultRepoWS", "There was an error while uploading document \"" + file.getName() + "\" - Status: " + status.getCode());
return model;
}
}
//If you want to test with CompanyHome first use this method instead of the NodeRef
#SuppressWarnings("unused")
private NodeRef getCompanyHome() {
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
serviceRegistry.getSearchService();
ResultSet rs = serviceRegistry.getSearchService().query(storeRef, SearchService.LANGUAGE_XPATH, "/app:company_home");
NodeRef parent = null;
try {
if (rs.length() == 0) {
throw new AlfrescoRuntimeException("Didn't find Company Home");
}
parent = rs.getNodeRef(0);
} finally {
rs.close();
}
return parent;
}
}
This java class is placed in the following folder:
{tomcat}\webapps\alfresco\WEB-INF\classes\org\example
Where org\example is the same as the package org.example. Now we have a class, now we need the configuration files as I call them:
customfileupload-context.xml
Which is located here
{tomcat}\shared\classes\alfresco\extension
You will also need these:
customfileupload.post.desc.xml
customfileupload.post.json.ftl
Which are located here
{tomcat}\shared\classes\alfresco\extension\webscripts\org\example
Noticed the folder? It's the same like the package mentioned earlier.
Contents of customfileupload-context.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN 2.0//EN'
'http://www.springframework.org/dtd/spring-beans-2.0.dtd'>
<beans>
<bean id="webscript.org.example.customfileupload.post" class="org.example.CustomFileUpload" parent="webscript">
<property name="ServiceRegistry" ref="ServiceRegistry" />
</bean>
</beans>
Contents of customfileupload.post.desc.xml:
<webscript>
<shortname>File Upload</shortname>
<description>Upload files to user home</description>
<url>/upload/fileupload.json</url>
<format default="json"/>
<authentication runas="admin">guest</authentication>
<transaction>required</transaction>
</webscript>
Contents of customfileupload.post.json.ftl:
<#escape x as jsonUtils.encodeJSONString(x)> { "resultRepoWS": "${resultRepoWS}" } </#escape>
This is it. With this you'll be able to upload a file to the Repository of Alfresco with Alfresco 5.

Registering MySQL DataSource with JNDI for Hibernate

I have hibernate which connect to database via JNDI datasource.
My purpose: registry DataSource with JNDI to test DAO layer.
Example
Hibernate config
<hibernate-configuration>
<session-factory name="MySessionFactory">
<property name="hibernate.connection.datasource">java:jdbc/MysqlMyDS</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mappings .... ->
</hibernate-configuration>
Get SessionFactory in test class :
Configuration cgf = new Configuration().configure("/META-INF/hibernate.cfg.xml");
SessionFactory iceleadsSessionFactory = cgf.buildSessionFactory();
As the result:
16:04:37,753 ERROR DatasourceConnectionProvider:78 - Could not find datasource: java:jdbc/MysqlIceleadsDS
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
To register JNOI I use example (http://www.roseindia.net/tutorial/java/jdbc/registeringthedatasourcewithjndi.html)
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class RegisteringJNDIWithDataSource {
private static void startRegistry() throws RemoteException {
System.out.println(LocateRegistry.getRegistry());
LocateRegistry.createRegistry(1059);
System.out.println("RMI registry Stared.");
}
private static InitialContext createInitialContextContext()
throws NamingException {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
properties.put(Context.PROVIDER_URL, "rmi://localhost:1059");
InitialContext initialContextcontext = new InitialContext(properties);
return initialContextcontext;
}
public static void main(String args[]) {
try {
startRegistry();
ConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("root");
((MysqlDataSource) dataSource).setServerName("192.168.10.13");
((MysqlDataSource) dataSource).setPort(3306);
((MysqlDataSource) dataSource).setDatabaseName("student");
InitialContext context = createInitialContextContext();
context.rebind("Source", dataSource);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Please suggest solution. Thanks!
Your code will work, if you correctly set jndi.properties. This file should be in classpath.
here is working example:
Server:
public static void main(String[] args) throws Exception{
LocateRegistry.createRegistry(1099);
ConnectionPoolDataSource dataSource = createDataSource("root", "");
InitialContext context = createContext();
context.bind("MysqlMyDS", dataSource);
System.out.println("context created!");
}
private static InitialContext createContext() throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext context = new InitialContext(env);
return context;
}
private static ConnectionPoolDataSource createDataSource(String username, String password) {
MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setServerName("localhost");
dataSource.setPort(3306);
dataSource.setDatabaseName("test");
return dataSource;
}
client:
hibernate.cfg.xml Note: datasource jndi name should be exactly as you set it by context.bind()
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">MysqlMyDS</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
</session-factory>
</hibernate-configuration>
jndi.properties (if you want, you can set it in code or with -D option)
java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory
java.naming.provider.url=rmi://localhost:1099
unit test
public class TestClient {
#Test
public void testCfg() throws Exception {
Configuration cgf = new Configuration().configure("/hibernate.cfg.xml");
cgf.buildSessionFactory();
}
}

How can i access any data in session inside spring aop logger class in java?

I am new to spring AOP and I am trying to implement logging for my Action class.Now i also want to save certain information in the DataBase while logging.For this to work I have certain data in my session that i need to access in my logger class but trying to access it, a "Null pointer exception occurs". Any help would be much appreciated...Thanx
LoggingInterceptor.java
package com.mcmc.utility;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import com.mcmc.hn.bean.UserInfo;
import com.mcmc.hn.dao.interfaces.UserManagementDao;
public class LoggingInterceptor implements MethodBeforeAdvice,SessionAware{ //, AfterReturningAdvice, ThrowsAdvice
private static Log log = null;
Map<String, Object> sesionMap=null;
HttpSession session = null;
UserManagementDao userManagementDao;
public LoggingInterceptor(){
}
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
log = LogFactory.getLog(arg2.getClass());
log.info("Beginning method: "+arg0.getName());
System.out.println("BEFORE>>>>>>>>>>>>>>>Beginning method: "+arg0.getName());
HashMap loggingDescription = new HashMap();
loggingDescription.put(new Integer(1),"This is a method to display List of Users");
loggingDescription.put(new Integer(2),"This is a method to display account Information of the logged-in User");
UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT); <-- THIS IS WHERE NULL POINTER EXCEPTION IS GENERATED.
String usrName = user.getFname() + " " + user.getLname();
String usrId = user.getUser_id();
String method="";
if(arg0.getName().equals("displayUser")){
method = (String) loggingDescription.get(1);
}else{
method = (String) loggingDescription.get(2);
}
userManagementDao.logInfo(method,usrName,usrId);
}
public void setSession(Map<String, Object> map) {
this.sesionMap = map;
}
}
applicationContext.xml---> The code for property
<!-- Bean configuration -->
<bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces" value="com.mcmc.hn.dao.interfaces.UserManagementDao">
</property>
<property name="target">
<ref local="userManagementDao" />
</property>
<property name="interceptorNames">
<list>
<value>theTracingBeforeAdvisor</value>
</list>
</property>
</bean>
<!-- Bean Classes -->
<!-- <bean id="userManagementDao" class="com.mcmc.hn.dao.UserManagementDaoImpl" /> -->
<!-- Advisor pointcut definition for before advice -->
<bean id="theTracingBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="theTracingBeforeAdvice" />
</property>
<property name="pattern">
<value>.*displayUser.*</value>
</property>
</bean>
<!-- Advisor pointcut definition for after advice -->
<!-- Advice classes -->
<bean id="theTracingBeforeAdvice" class="com.mcmc.utility.LoggingInterceptor" />
UserManagementAction.java--->Action Class
public class UserManagementAction extends ActionSupport implements ModelDriven<UserInfo>, RequestAware,SessionAware, ServletResponseAware, ServletRequestAware {
private UserInfo userInfo = new UserInfo();
private UserAddress userAddress = new UserAddress();
UserManagementDao userManagementDao;
KeywordCategoryDao keywordCategoryDao;
HttpServletRequest request = null;
HttpServletResponse response = null;
HttpSession session = null;
/*ProxyFactoryBean proxyBean = null;*/
Map<Long, Object> userSessionMap=new HashMap<Long, Object>(); // Map is used to hold the user session reference in Servlet Context
String userName="";
String password="";
String remStatus="";
Map<String, Object> reqMap=null;
Map<String, Object> sesionMap=null;
String usrName=""; //Variables for logging
String usrId="";
/**
* Calls a function to retrieve the values from Database.
* #return SUCCESS in oder to load the JSP page.
*/
#SuppressWarnings({ "unchecked" })
public String displayUser(){
UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT);
usrName = user.getFname() + " " + user.getLname();
usrId = user.getUser_id();
String method="This is a method to display List of Users";
userManagementDao.logInfo(method,usrName,usrId);
ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:../../WEB-INF/applicationContext.xml");
UserManagementDao userManagementDao=(UserManagementDao) appContext.getBean("proxyBean");
List<UserInfo> Lst = userManagementDao.displayUser();
reqMap.put("userList", Lst);
return SUCCESS;
}
public String login(){
ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:../../WEB-INF/applicationContext.xml");
UserManagementDao userManagementDao=(UserManagementDao) appContext.getBean("proxyBean");
String returnStatus = SUCCESS;
session = request.getSession();
long usr_id;
long roleId;
int count=0,flag=0;
Map<Long, Object> userMap=new HashMap<Long, Object>();
int status=0;
if(remStatus.equals("on")){
status=1;
}else{
status=0;
}
session.setAttribute("status", status);
UserInfo user=userManagementDao.login(userName, password);
userMap=(Map<Long, Object>)session.getServletContext().getAttribute(MCMCConstants.USER_SESSION_REFERENCE_MAP); //Retrieving userMap from Servlet Context with User Session Info
if( userMap!= null){
if(userMap.containsKey(user.getId())){
flag=1;
}
}
if(user!=null && flag==0 ){
count=1;
sesionMap.put(MCMCConstants.USER_INFO_OBJECT, user);<--THIS IS THE DATA THAT IS IN SESSION THAT I NEED TO ACCESS IN LOGGINGINTERCEPTOR.java
usr_id=user.getId();
roleId=user.getRoleId();
//System.out.println("USERID::::::"+roleId);
sesionMap.put(MCMCConstants.USER_INFO_ID,usr_id);
sesionMap.put(MCMCConstants.USER_ROLE_ID,roleId);
// Loading Constants into Session
SessionUtility.loadCategoryList(sesionMap, keywordCategoryDao);
if(status==1){
Cookie cookie = new Cookie ("loginData",userName + "|" + password+"?" + status);
response.addCookie(cookie);
}
userSessionMap.put(user.getId(), user.getUser_id());
session.getServletContext().setAttribute(MCMCConstants.USER_SESSION_REFERENCE_MAP, userSessionMap); //Setting userSessionMap to ServletContext
returnStatus=SUCCESS;
}else{
returnStatus= "input";
}
return returnStatus;
}
/* Getter And Setter Methods as required */
}
Why will this not be null ? UserManagementDao had not been injected into LoggingInterceptor nor has it been initialized . In fact it has been commented out in the configuration file
<!--<bean id="userManagementDao" class="com.mcmc.hn.dao.UserManagementDaoImpl"/>-->
Unless there is an #Repository in userManagementDao and a component scan, it is not even a Spring bean . Please read throught his for reference http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#beans-introduction
you declared
Map<String, Object> sesionMap=null;
Null pointer was generated here:
UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT);
using the same sessionMap here ...thats why getting nullpointer.

Categories