I've managed to run Drools with CDI using version 6.3.0.Final, CDI version 1.2 and Weld version 2.30.Final. Using code sample below.
https://github.com/Salaboy/drools-workshop/tree/master/my-first-drools-project
public class App {
#Inject
#KSession
private KieSession kSession;
public void bootstrapDrools() {
// The KieSession was injected so we can use it now
kSession.insert("Hi There!");
int rulesFired = kSession.fireAllRules();
System.out.println(">>> Rules Fired: "+rulesFired);
}
public static void main(String[] args) {
//Boostrap the CDI container, in this case WELD
Weld w = new Weld();
WeldContainer wc = w.initialize();
App app = wc.select(App.class).get();
app.bootstrapDrools();
w.shutdown();
}
}
Now I'm trying to do the same using drools 7.2.0 final.
But I can't get it to work. Any ideas?
As I answer in my blog post
you need to add the following dependency in drools 7 to get those examples working:
org.drools
drools-cdi
${kie.version}
Related
How to display the content of jsp file in browser through Liferay Struts Action (DXP 7.1)? I use the execute method with parameters of HttpServletRequest and HtttpServletResponse, and place the jsp file in resources/META-INF/resources, but it is not showing in the browser, also no any errors in microservice logs.
The project, where I'm implementing new functionality, use Scala and SBT, so I can't use maven or something else.
I tried to follow the official documentation of overriding Liferay Struts action on their page, but it describe case for DXP 6.x version, despite this, I still tried the described approach using struts-config.xml, but it did not help.
The latest iteration of implementation based on of using requestDispatcher, but I'm not sure this is right:
class RedirectInfoAction extends BaseStrutsAction with LiferayLogSupport {
override def execute(
originalStrutsAction: StrutsAction,
request: HttpServletRequest,
response: HttpServletResponse
): String = {
log.info("Strut action execution started...")
getUserId(request) match {
case Some(_) =>
val servletContext = request.getSession().getServletContext
val requestDispatcher = servletContext.getRequestDispatcher("/page.jsp")
requestDispatcher.include(request, response)
case None =>
response.sendError(401, "The user is not authorized")
}
""
}
It all depends on how you deploy the code: You're just posting the code here, but if you look at the blade sample for a struts action, you'll see an additional critical part, which is the #Component section (granted, this requires using Java), as well as the #Reference to the servlet context.
You can also manually register such a service to the OSGi runtime, but that requires quite a lot of internal OSGi knowledge.
#Component(
immediate = true, property = "path=/portal/blade",
service = StrutsAction.class
)
public class BladeStrutsAction extends BaseStrutsAction {
public String execute(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)
throws Exception {
RequestDispatcher requestDispatcher =
_servletContext.getRequestDispatcher("/html/portal/blade.jsp");
requestDispatcher.forward(httpServletRequest, httpServletResponse);
return null;
}
#Reference(target = "(osgi.web.symbolicname=blade.strutsaction)")
private volatile ServletContext _servletContext;
}
(see the rest of the project as well: There's a bnd.bnd that contains information for the osgi.web.symbolicname configuration, etc)
I'm trying to implement permissions (resources & models ones) on Liferay 7.1. I've noted that BaseModelPermissionChecker is now deprecated (so I should not make my permission classes extending it). So I tried to do as indicated in Liferay official tutorial
However, when deploying my osgi services, OSGI container indicates the following error (for model and resource permission):
ERROR [Refresh Thread: Equinox Container: 80bbc977-c7cb-0018-1000-82f21869d6ec][com_liferay_training_space_gradebook_service:97] [com.a.b.c.service.permission.MyEntityPermission(3887)] Field _portletResourcePermission in component class com.a.b.c.service.permission.MyEntityPermission must not be static.
Have you got that error ? Did I skip something ?
EDIT
Component created:
#Component(immediate = true, service = {})
public class BookResourcePermission {
public static final String ADD_BOOK = "ADD_BOOK";
#Reference(target = "(resource.name=com.x.y.mybook.model)", unbind = "-")
protected static void setPortletResourcePermission(PortletResourcePermission portletResourcePermission) {
_portletResourcePermission = portletResourcePermission;
}
public static boolean contains(PermissionChecker permissionChecker, long groupId, String actionId) {
return _portletResourcePermission.contains(permissionChecker, groupId, actionId);
}
private static PortletResourcePermission _portletResourcePermission;
}
Thanks !
I'm trying to implement a ModelListener in Liferay 7. When I'm updating a JournalArticle in the UI, I would like to add a small behaviour and some logging.
#Component(immediate = true, service = ModelListener.class)
public class DownloadsListener extends BaseModelListener<JournalArticle> {
#Reference
private StructureService structureService;
#Reference
private DLFileEntryLocalServiceUtil fileEntryLocalService;
private static final Log LOG = LogFactoryUtil.getLog(DownloadsListener.class);
#Override
public void onAfterUpdate(JournalArticle model) throws ModelListenerException {
LOG.error("UPDATES!");
LOG.error("UPDATES!");
super.onAfterUpdate(model);
if(structureService.isNieuwsArticle(model)) {
}
getFileFromURL("/documents/82177/0/file.pdf/0d10338c-8ca1-c5b7-cc4b-011bef1ee759");
}
private DLFileEntry getFileFromURL(String url) {
String[] splittedURL = StringUtil.split(url, '/');
return null;
}
}
But this code is never triggered when I'm updating an article. I'm not hitting my debug point in the onAfterUpdate method, and I'm not seeing any logging. The OSGI Module is deployed correctly. What am I missing?
The issue was that my ModelListener was not being correctly deployed in the OSGi container. (Although Eclipse / Apache Gogo told everything was fine).
OSGi could not resolve:
#Reference
private DLFileEntryLocalServiceUtil fileEntryLocalService;
Changed it to
#Reference
private DLFileEntryLocalService fileEntryLocalService;
Now the behaviour is correctly. It's a shame that there is ZERO feedback by Apache GoGo.
My Question is near the same than Packaging Facelets files (templates, includes, composites) in a JAR. Wich was full quallyfied awnsered by BalusC.
But I'll go a bit farther. Since JBoass AS 7 the ClassLoading in JB has changed. Now there is a strict Modul-ClassLoading and it works so far ;)
I have a WAR-file with an jboss-deployment-structure.xml plus one JAR-file with some CDI-Beans and xhtml-files in /META-INF/resources/modul.
I deploy the two "projects" seperately (so the JAR is not packaged into the WAR both ar copyed in JBOSS_HOME/standalone/deployment).
The WAR references the JAR as a Modul through the jboss-deployment-structure.xml.
I'm able to Inject CDI-Beans from the JAR but I can't reach the xhtml-files in /META-INF/resources/modul/. Everytime I try to load CONTEXT_ROOT/modul/modul.xhtml I get a warning from JB :
[javax.enterprise.resource.webcontainer.jsf.application] (default task-5) JSF1064: Ressource /modul/modul.xhtml can not be found or served.
If I package the JAR into the WAR (WEB-INF/lib) it dosen't work ither.
I provided a faces-config.xml under META-INF/resources
Did I miss somethink?
reggards
I use WildFly 9.0.1 Final and the Provided J2EE implementations.
I found a solution. The problem is, that JBoss doesn't scan the JAR if it is not packaged in WEB-INF/lib.
In my initial question I have wrote "If I package the JAR into the WAR (WEB-INF/lib) it doesn't work ither." thats not true i just didn't recognise that i placed my files in /META-INF/modul instead of /META-INF/resources/modul. I moved the files and it start to work if i placed the JAR in WEB-INF/lib.
But the problem with an separate deployment still persisted.
Now I use an custom ResourceHandler (JSF 2.2 way) and it work like a charm.
All my moduls have to implement a interface. In that way I'm able to Inject all these instances by CDI. Now i itterate over all moduls and look for my resource. The ResourceHandler is placed in the WAR not in the single JAR's. In that way i just have to implement it onetimes.
Here is my code example:
public class ExternalResourceHandler extends ResourceHandler {
#Inject
#Any
Instance<ModulDescriptor> moduls;
private ResourceHandler parent;
private String basePath = "META-INF/resources";
public ExternalResourceHandler() {
}
public ExternalResourceHandler(ResourceHandler parent) {
this.parent = parent;
}
#Override
public ViewResource createViewResource(FacesContext context, String resourceName) {
System.out.println("Invoked for: " + resourceName);
ViewResource resource = parent.createViewResource(context, resourceName);
if (resource == null) {
System.out.println("Parent failed");
if (moduls != null) {
for (ModulDescriptor mod : moduls) {
URL url = mod.getClass().getClassLoader().getResource(basePath + resourceName);
if (url != null) {
return new ViewResource() {
#Override
public URL getURL() {
return url;
}
};
}
}
} else {
System.out.println("INJECTION IS NULL");
}
}
return resource;
}
//followed by all other overriden methods wich return null ;)
}
Thanks to #BalusC for this awnser wich does the clue in the second step.
I am using Spring 3.1 with annotations to create a DataSource that uses embedded HSQL.
#Bean
public DataSource dataSource() throws Exception {
EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
bean.setDatabaseType(EmbeddedDatabaseType.HSQL);
bean.afterPropertiesSet();
DataSource object = bean.getObject();
return object;
}
I am also configuring a SessionFactory like this
#Bean
public SessionFactory sessionFactory() {
SessionFactory sessionFactory = new LocalSessionFactoryBuilder(dataSource)
.setNamingStrategy(namingStrategy())
.addProperties(hibernateProperties)
.addAnnotatedClass(Some.class)
.buildSessionFactory();
logger.info("Created session factory: " + sessionFactory + " with dataSource: " + dataSource);
return sessionFactory;
}
The problem is if I create some other beans using #Component in which I populate the database, the SQL script fails because the database has not been created. My hibernate.properties includes the following line to generate DDL
properties.put("hibernate.hbm2ddl.auto", "create-drop");
So it is some sort of ordering issue with bean creating. However this problem only occurs in Linux (Kubuntu 12.04) and not on Windows 7!
I have worked out that in my #Component bean which populates the database I had to add the #DependsOn annotation like this
#Component
#DependsOn({"dataSource", "sessionFactory"})
public class DevSqlPopulator {
...
}
I think the issue is that you are calling a InitializingBean method afterPropertiesSet method yourself instead of letting Spring call it once all the properties are cleanly set. Try doing this instead:
#Bean
public EmbeddedDatabaseFactoryBean dataSource() throws Exception {
EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
bean.setDatabaseType(EmbeddedDatabaseType.HSQL);
return bean;
}
Now it is a clean factory bean and Spring will take care of the rest of the lifecycle.