Unable to call policy/behaviour in alfresco 5 - java

Previously I was using alfresco 4.1.3 in which I have done too many customization Like. policy,custom action etc.
Now I have migrated my code to alfresco 5.01 and deployed it successfully. almost functionality are working fine but I am not able to call custom policy/behaviour.
Below is sample code.
public class OnCreateObjectPolicy extends AbstractConfigurablePolicy implements NodeServicePolicies.OnCreateNodePolicy{
#Override
public void onCreateNode(ChildAssociationRef childAssocRef) {
System.out.println("************Create node called successfully");
}
#Override
public void bindBehavior() {
System.out.println("************bindBehavior called successfully");
}
}
Any one can help?

You need to register your policy/class in (any of) the context xml files as well. Just having them in your module/jar/class does not register them yet automatically.
I'm sure you did that in your 4.1.3 installation, just copy it over.
Sample:
<bean id="onCreateNodePolicy" class="com.mycompany.alfresco.repo.policies.OnCreateNodePolicy" init-method="init">
<property name="ServiceRegistry" ref="ServiceRegistry" />
...
</bean>

Related

Property does not get filled within dependency jar (Spring 3 / XML)

We have a Spring managed application that uses another jar as a dependency, which contains a Spring managed service class, that need to use some value injected from property file
The main application with the Spring context setup
public static void main(String[] args) {
GenericXmlApplicationContext appContext = new GenericXmlApplicationContext("applicationContext.xml");
SomeClass someClass = (SomeClass) appContext.getBean("someClass");
someClass.someMethod();
...
The class which calls the service from the dependent jar
public class SomeClass {
private ServiceFromTheOtherJar serviceFromTheOtherJar;
public SomeClass(ServiceFromTheOtherJar serviceFromTheOtherJar) {
this.serviceFromTheOtherJar = serviceFromTheOtherJar;
}
public void someMethod() {
serviceFromTheOtherJar.call();
...
The applicationContext.xml of the main app
<bean name="serviceFromTheOtherJar" class="com...ServiceFromTheOtherJar"/>
<bean name="someClass" class="com...SomeClass">
<constructor-arg ref="serviceFromTheOtherJar"/>
</bean>
The service class in the dependent jar
public class ServiceFromTheOtherJar {
private String someFieldWeWantToFillFromPropertyFile;
public void setSomeFieldWeWantToFillFromPropertyFile(String someFieldWeWantToFillFromPropertyFile) {
this.someFieldWeWantToFillFromPropertyFile = someFieldWeWantToFillFromPropertyFile;
}
public void call() {
//we would like to use the filled someFieldWeWantToFillFromPropertyFile here
...
And of course we have an application.properties file in the dependent jar, that contains the property value that we would like to inject into someFieldWeWantToFillFromPropertyFile
Now we can add the dependent jar as a dependency to the main app; when the main app is being executed then its Spring context is getting set up all right, and ServiceFromTheOtherJar.call() method gets called as expected; however someFieldWeWantToFillFromPropertyFile does not get filled from the property file whatever we tried so far (e.g. #PropertySource({"application.properties"}), Environment.getProperty(...) etc.)
Restrictions
We have Spring 3 version in both jars, and that has to remain so due to the deployment environment; so Spring 4 solutions are out of question
As you see above, the main app currently uses GenericXmlApplicationContext and changing that seem to indicate a significant rewriting of the application. Therefore e.g. it seemed to be not possible to use #Service annotation on ServiceFromTheOtherJar because it caused BeanCreationException during the execution and context setup
To read values from property file you have to add the following bean to applicationContext.xml.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:application.properties" />
</bean>
Assuming the application.properties-file contains a definition like this
myValue=Hello World
the definition of the service-bean should by extended like this
<bean name="serviceFromTheOtherJar" class="com...ServiceFromTheOtherJar">
<property name="someFieldWeWantToFillFromPropertyFile" value="${myValue}" />
</bean>
Now Spring will look for the application.properties-file in classpath and set the attribute of the service bean according to myValue.

How to execute insert query on deploy of java ee application?

I have a java ee application with a web application and ejb module. There is some tables I want to be populated automatically when deployed. I have the entities set up and the sql file with the queries to be executed.
I feel it may be something to do with the persistence.xml file but not sure what to include. So, how do I get my server(wildfly) to execute the sql queries on deploy, similar to the way that you can specify the persistence.xml to drop and create as the table generation strategy.
If it makes a difference, I am using postgres for my database provider, java ee 7 and wildfly 8.2.0.Final for my server.
The solution I was looking for was like this, it belongs in the persistence.xml properties section.
<property name="javax.persistence.sql-load-script-source" value="META-INF/sql/data.sql" />
Database Schema Creation This link is useful for such techniques.
You could write a class extends from ServletContextListener which calls your method from the contextInitialized() method. You attach the listener to your webapp in web.xml, e.g.
<listener>
<listener-class>com.packages.Listener</listener-class>
</listener>
ServletListener code:
package com.packages;
public class Listener implements javax.servlet.ServletContextListener {
#Override
public void contextInitialized(ServletContext context) {
...
}
}
You can use Spring JDBC DataSource initializer feature or Instead of configuring DataSourceInitializer, you can use spring’s custom element jdbc:initialize-database element where jdbcnamespace xmlns:jdbc is set to http://www.springframework.org/schema/jdbc. You just need to point attribute data-source to the DataSource bean and configure the scripts using the child element jdbc:script and location attribute. For example, we can achieve the same result using the below configuration:
<jdbc:initialize-database data-source="dataSource" enabled="true">
<jdbc:script location="classpath:db-schema.sql" />
<jdbc:script location="classpath:db-load-data.sql" />
</jdbc:initialize-database>
At the very least you could use a servlet ContextListener to populate the database upon context start.
http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContextListener.html
I've done such a trick for prototype applications in the past; just make sure to check if data doesn't already exist to prevent populating the database with duplicate data upon redeployments.
A rough example:
#WebListener
public class MyContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce){
// create manually, inject, whatever you're using
EntityManager em = ...;
Number cnt = (Number) em.createQuery("select count(d) from Data d").getSingleResult();
if(cnt.intValue() == 0){
insertFixtureData(em);
}
}
public void contextDestroyed(ServletContextEvent sce){
}
private void insertFixtureData(EntityManager em){
// insert data
}
}

Unannotated methods still added in JConsole (JMX)

I have a problem managing a class in JMX. I exposed it to JMX, and set the manageable methods and attributes of my class by adding annotation but when I open my bean in the JConsole it exposed all the methods and it prints me the output like illustrated in the screenshot below :
I can still use my exposed methods without any problems but it's a bit annoying to allways have this error's window popping and to have to scroll down to find my exposed methods.
Here is how I declared my bean :
<bean name="MBeanExporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="SmartTrade:name=tickEngine" value-ref="aggregationEngine" />
</map>
</property>
</bean>
with aggregationEngine is a reference to my class :
<bean name="aggregationEngine" class="com.smarttrade.tick.engine.TickEngine">
<!-- list of properties .... -->
</bean>
and here a part of my class where you can see that setTickDataReader(..) is not exposed but still appears in the JConsole, and also how I made my annotations for the good methods :
public void setTickDataReader(TickDataReader tickDataReader) {
this.tickDataReader = tickDataReader;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
#ManagedOperation(description = "Activate or deactivate tick data")
#ManagedOperationParameters({ #ManagedOperationParameter(name = "enable", description = "boolean") })
public void enableTickData(boolean enable) {
this.enabled = enable;
if (enabled) {
init();
} else {
unsubscribe();
}
}
Any idea of where it can come from ?
Thanks in advance.
The org.springframework.jmx.export.MBeanExporter by default uses the org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler. Which effectively exposes all the public methods to JMX.
To only have your annotated methods exported either switch to using the org.springframework.jmx.export.annotation.AnnotationMBeanExporter or set the namingStrategy and assembler property in such a way that it uses annotation processing (which is basically what the org.springframework.jmx.export.annotation.AnnotationMBeanExporter also does).

Cannot export Interface in OSGI

I am trying to export an interface (ITestRunable) I am sure I have linked everything correctly in the context.xml and osgi.xml files but it just wont work. The error is:
Application context initialization for 'me.overlaymanagement' has timed out waiting for (objectClass=me.overlaymanagement.testing.ITestRunable)
Any got any idea?
Code: Spring files for ITestRunable
context.xml
<bean id="testingRunable" class="me.overlaymanagement.testing.Testing"
init-method="startUp" destroy-method="shutDown">
</bean>
osgi.xml
<osgi:service id="testingRunable" ref="testingRunable">
<osgi:interfaces>
<value>me.overlaymanagement.testing.ITestRunable</value>
</osgi:interfaces>
</osgi:service>
Spring files for import into main system
context.xml
<bean id="overlaymanagementsystem"
class="me.overlaymanagementsystem.OverlayManagementSystem"
init-method="startUp" destroy-method="shutDown">
<property name="testingRunable" ref="testingRunable"/>
</bean>
osgi.xml
<osgi:reference id="testingRunable" cardinality="1..1"
interface="me.overlaymanagement.testing.ITestRunable">
</osgi:reference>
Gettors and Settors for ITestRunable within main system
public class OverlayManagementSystem{
...
protected ITestRunable testingRunable;
....
public ITestRunable getTestRunable() {
return testingRunable;
}
public void setTestRunable(ITestRunable testingRunable) {
this.testingRunable = testingRunable;
}
}
can anyone see a problem?
Ok so i managed to find the poblem. It was two things actually:
It didnt like that i had set the bean id and the service id where the ITestRunable code is located. Once i changed the service id name to something different it gave me a new error about invalid gettors and settors, which is the next point.
after some research i found the your gettor and setter for the imported service must be that same as the beans property name for that service. notice in my bean its:
< property name="testingRunable" .../>
and the get/set are defined as: "getTestRunable/setTestRunable". Changing these to getTestingRunable and setTestingRunable solved the problem for me.
I hope this helps others.

Properties Handling With Seam In A Spring-like fashion

One of the Spring features I like the most is how you can handle properties loaded from a file. You only to need to set up a bean like the one below
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/app.properties" />
</bean>
Now you are able to inject the properties values loaded from the app.properties into your beans by using either xml (see below) or annotations.
<bean class="com.example.PersonDaoImpl">
<property name="maxResults" value="${results.max}"/>
</bean>
results.max is one of the properties.
I'm great fan of this feature because it enables me to create quite flexible applications that I can turn on/off some functionality by only changing one property - without redeploying the app.
Now I'm working with JBoss Seam and I've been struggling to find a way to do something similar using this framework.
Does anyone know how to do it? If not, does anyone have any idea of how I could handle properties in a nice way using Seam (I have seen some ways - but none of them were good enough).
Thanks.
If there's no appropriate way using you software stack (is there really no dependency injection!?). I'd say: Use Google Guice (https://code.google.com/p/google-guice/ , https://code.google.com/p/google-guice/wiki/Motivation?tm=6)!
Bad thing with Guice: You may have to read a lot to understand how it works, and what you can do with it. But after it's running you will simply inject your Properties object where you need it:
class YourClass {
#Inject Properties myProperties;
#Inject
public YourClass() { ... }
public void someMethod() {
use the property
}
}
or if you need the properties inside the constructor, you could also do:
class YourClass {
final Properties myProperties;
#Inject
public YourClass(Properties myProperties) {
this.myProperties = myProperties;
}
public void someMethod() {
use the property
}
}
Using Guice maybe enforces you to refactor your whole app.
But if you already have some DI framework, you should simply use that :)

Categories