cancel
Showing results for 
Search instead for 
Did you mean: 

JCR - aplication-context.xml?

anita_platos
Champ in-the-making
Champ in-the-making
I want to try to make run the program of the standard samples "SDK FirstJCRClient". It starts like this:


public static void main(String[] args) throws Exception
{
     ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");

     Repository repository = (Repository)context.getBean("JCR.Repository");

     Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
   


I don´t understand the concept of using application-context.xml. Do I have to put there information like the host name and port?. Somebody can tell me what do I have to put in that file to make run the program?. I have been looking it on internet but I don´t understand how does it work!
Thank you so much!
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
That method tells spring to load the beans in alfresco/application-context.xml which is found on the classpath. application-context also pulls in other files sufficient to start alfresco.

To set the host-name or port you would add them to a properties file.   Probably alfresco-global.properties that is loaded after all the other beans and can therefore contain your settings. 

anita_platos
Champ in-the-making
Champ in-the-making
Thank you Rogers!. OK, there is an alfresco-global.properties. I have put exactly the same information that I have in my
alfresco-global.properties in my alfresco repository, that´s:


dir.root=D:/ANA/ALFRESCO/alfresco_data
db.username=alfresco
db.password=alfresco
db.name=alfresco
db.port=3307
db.driver=org.gjt.mm.mysql.Driver
db.url=jdbc:mysql://${alfresco.host}:${db.port}/${db.name}
alfresco.rmi.services.port=50500
alfresco.rmi.services.host=localhost



I copy/paste you the Java code - it´s only one java class -, it´s the SDK Sample downloaded from the
alfresco web page, exactly the same without modify absolutely nothing. Moreover, I don´t understand
where does it should read that alfresco-global.properties in this java code, I don´t understand
nothing, it´s so weird!

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.sample;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



/**
* Simple client example demonstrating the use of the Alfresco JCR (JSR-170) API.
*
* The client creates a content node in the "Company Home" folder.  The content
* may be viewed and operated on within the Alfresco Web Client.  Note: the web client
* will need to be re-started after executing this sample to see the changes in
* effect.
*
* This client demonstrates the "Embedded Repository" deployment option as described
* in the Alfresco Respotiory Architecture docucment -
* http://wiki.alfresco.com/wiki/Alfresco_Repository_Architecture
*/
public class FirstJCRClient
{
   
    public static void main(String[] args)
       throws Exception
   {
       
       // access the Alfresco JCR Repository (here it's via programmatic approach, but it could also be injected)
       ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
       Repository repository = (Repository)context.getBean("JCR.Repository");
   
       // login to workspace (here we rely on the default workspace defined by JCR.Repository bean)
       Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
   
       try
       {
           // first, access the company home
           Node rootNode = session.getRootNode();
           Node companyHome = rootNode.getNode("app:company_home");
   
           // create the content node
           String name = "JCR sample (" + System.currentTimeMillis() + ")";
           Node content = companyHome.addNode("cm:" + name, "cm:content");
           content.setProperty("cm:name", name);

           // add titled aspect (for Web Client display)
           content.addMixin("cm:titled");
           content.setProperty("cm:title", name);
           content.setProperty("cm:description", name);

           //
           // write some content to new node
           //
           content.setProperty("cm:content", "The quick brown fox jumps over the lazy dog");
          
           //
           // To set the content mime type, we need to use an Alfresco native service
           // as there isn't an equivalent call in JCR
           //
           setMimeType(context, content, MimetypeMap.MIMETYPE_TEXT_PLAIN);

           // save changes
           session.save();
       }
       finally
       {
           session.logout();
           System.exit(0);
       }
   }   

   
    /**
     * Demonstrates the mixed use of JCR API calls and Alfresco Foundation API calls.
     *
     * Here, the Foundation API is used to set the mimetype of the content.
     *
     * @param context  application context
     * @param node   the JCR Node to adjust
     * @param mimeType  the mimetype to set
     * @throws RepositoryException
     */
   private static void setMimeType(ApplicationContext context, Node node, String mimeType)
       throws RepositoryException
   {
      // retrieve service registry
        ServiceRegistry serviceRegistry = (ServiceRegistry) context.getBean(ServiceRegistry.SERVICE_REGISTRY);
        NodeService nodeService = serviceRegistry.getNodeService();
      
       // convert the JCR Node to an Alfresco Node Reference
       NodeRef nodeRef = JCRNodeRef.getNodeRef(node);
   
       // retrieve the Content Property (represented as a ContentData object in Alfresco)
       ContentData content = (ContentData)nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
      
       // update the Mimetype
       content = ContentData.setMimetype(content, mimeType);
       nodeService.setProperty(nodeRef, ContentModel.PROP_CONTENT, content);
   }

}



And this is the exception it appears:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'encryptionKeysRegistry' defined in class path resource [alfresco/encryption-context.xml]: Cannot resolve reference to bean 'transactionService' while setting bean property 'transactionService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionService' defined in class path resource [alfresco/core-services-context.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [alfresco/hibernate-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [alfresco/hibernate-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
   at org.alfresco.sample.FirstJCRClient.main(FirstJCRClient.java:59)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionService' defined in class path resource [alfresco/core-services-context.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [alfresco/hibernate-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [alfresco/hibernate-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
   … 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [alfresco/hibernate-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [alfresco/hibernate-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
   … 25 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [alfresco/hibernate-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
   … 35 more
Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
   at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:59)
   at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
   at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
   at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
   at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2073)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1298)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
   at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
   … 42 more

mrogers
Star Contributor
Star Contributor
is your mysql database running?   And is your classpath set correctly.   alfresco-global.properties need to be ob the classpath, so it should be alongside FirstJCRClient.class.   Or enen better on the "shared" external directory.

kaynezhang
World-Class Innovator
World-Class Innovator
You can copy alfresco-global.properties.sample to your classpath and rename it to alfresco-global.properties. then uncomment following settings(Or you can create alfresco-global.properties with following settings in your classpath)

db.username=alfresco
db.password=alfresco
db.driver=org.gjt.mm.mysql.Driver
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
db.url=jdbc:mysql://localhost/alfresco?useUnicode=yes&characterEncoding=UTF-8
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.