Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
minhdv committed Sep 16, 2015
2 parents e2c5b51 + 3a9ab3c commit afb6917
Show file tree
Hide file tree
Showing 26 changed files with 453 additions and 91 deletions.
26 changes: 26 additions & 0 deletions commons-api/pom.xml
Expand Up @@ -55,4 +55,30 @@
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>**/ExoEntityProcessor*</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-everything-else</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2015 eXo Platform SAS.
*
* This 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 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.exoplatform.commons.api.persistence;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Describe a class as a JPA entity managed by eXo Platform
*
* @author <a href="bdechateauvieux@exoplatform.org">Benoit de Chateauvieux</a>
* @version $Revision$
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ExoEntity {
}
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2003-2015 eXo Platform SAS.
*
* This program 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.
*
* This program 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 this program. If not, see http://www.gnu.org/licenses/ .
*/
package org.exoplatform.commons.api.persistence;

import java.io.IOException;
import java.io.Writer;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

/**
* At compile time, every time a Maven module that uses @ExoEntity is compiled, the ExoEntityProcessor is invoked.
* This Annotation Processor (see JSR 269) creates an index that contains the list of all
* the JPA entities annotated with @ExoEntity of the module.
* This index is stored in the file “exo-jpa-entities/entities.idx” of the generated jar.
*
* Created by The eXo Platform SAS
* Author : eXoPlatform exo@exoplatform.com
* 7/22/15
*/
@SupportedAnnotationTypes("org.exoplatform.commons.api.persistence.ExoEntity")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class ExoEntityProcessor extends AbstractProcessor {
/** Path to the generated entities.idx file **/
public static final String ENTITIES_IDX_PATH = "exo-jpa-entities/entities.idx";

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Writer writer = null;
if ( (roundEnv != null) &&
(roundEnv.getElementsAnnotatedWith(ExoEntity.class)!=null) &&
(roundEnv.getElementsAnnotatedWith(ExoEntity.class).size()>0)) {
try {
FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", ENTITIES_IDX_PATH);
writer = file.openWriter();
for (Element element : roundEnv.getElementsAnnotatedWith(ExoEntity.class)) {
writer.append(element.asType().toString() + "\n");
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Exo JPA entity detected: " + element.asType());
}
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error while processing @ExoEntity: " + e.getMessage());
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error while processing @ExoEntity: " + e.getMessage());
}
}
}
}
return true; // No need to process these annotations again
}
}
Expand Up @@ -28,11 +28,11 @@
* REQUIRED. Support a current transaction, create a new one if none exists.
* Analogous to EJB or Spring transaction attribute of the same name.
*
* @see org.exoplatform.commons.api.persistence.TransactionalAspect
* @see org.exoplatform.commons.api.persistence.ExoTransactionalAspect
* @author <a href="bdechateauvieux@exoplatform.org">Benoit de Chateauvieux</a>
* @version $Revision$
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Transactional {
public @interface ExoTransactional {
}
@@ -0,0 +1,2 @@
# Define processors that should be used for Annotation Processing
org.exoplatform.commons.api.persistence.ExoEntityProcessor
41 changes: 27 additions & 14 deletions commons-component-common/pom.xml
Expand Up @@ -175,7 +175,12 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.12.Final</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
Expand All @@ -192,30 +197,38 @@
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.gtmpl</include>
<include>**/*.Processor</include>
<include>**/*.Integrator</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<id>weave-classes</id>
<goals>
<goal>ajc</goal>
</goals>
<phase>compile</phase>
<configuration>
<classesDirectory>target/classes</classesDirectory>
</configuration>
</execution>
<execution>
<id>weave-test-classes</id>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
<goal>ajc</goal>
</goals>
<phase>test-compile</phase>
<configuration>
<classesDirectory>target/test-classes</classesDirectory>
</configuration>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<showWeaveInfo>false</showWeaveInfo>
<includes>
<include>**/persistence/**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Expand Up @@ -80,6 +80,10 @@ public void save(NotificationInfo notification) {
Integer current = data.build();
exoWebNotificationCountCache.put(key, new IntegerData(current + 1));
}

ListWebNotificationsKey listWebNotificationsKey = ListWebNotificationsKey.key(notification.getTo(), true);
ListWebNotificationsData listWebNotificationsData = new ListWebNotificationsData(listWebNotificationsKey);
exoWebNotificationsCache.put(listWebNotificationsKey, listWebNotificationsData);
moveTopPopover(notification);
moveTopViewAll(notification);
//
Expand All @@ -100,6 +104,9 @@ public void updateRead(String notificationId, boolean isRead) {
if (infoData != null) {
infoData.updateRead(isRead);
}
if (isRead) {
clearWebNotificationCache(notificationId);
}
}

public void updateAllRead(String userId) throws Exception {
Expand All @@ -117,6 +124,7 @@ private void updateCacheByUser(String userId, boolean isUpdateRead) {
if (userId.equals(ntf.getTo())) {
if (isUpdateRead) {
webData.updateRead(true);
clearWebNotificationCache(ntf.getId());
} else {
removeIds.add(ntf.getId());
}
Expand Down Expand Up @@ -181,8 +189,7 @@ public boolean remove(String notificationId) {
//
removeViewAll(notification);
//
WebNotifInfoCacheKey key = WebNotifInfoCacheKey.key(notificationId);
exoWebNotificationCache.remove(key);
clearWebNotificationCache(notificationId);
//clear badge number in for notification's TO user.
clearWebNotificationCountCache(notification.getTo());
//
Expand Down Expand Up @@ -337,6 +344,15 @@ public void clearWebNotificationCountCache(String userId) {
exoWebNotificationCountCache.remove(key);
}

/**
* Clear the notification from the cache.
* @param notifId
*/
public void clearWebNotificationCache(String notificationId) {
WebNotifInfoCacheKey key = WebNotifInfoCacheKey.key(notificationId);
exoWebNotificationCache.remove(key);
}

public void moveTopPopover(NotificationInfo notification) {
ListWebNotificationsKey userPopoverKey = ListWebNotificationsKey.key(notification.getTo(), true);
ListWebNotificationsData listData = getWebNotificationsData(userPopoverKey);
Expand Down
Expand Up @@ -18,15 +18,19 @@
*/
package org.exoplatform.commons.persistence.impl;

import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.apache.commons.lang.StringUtils;
import org.exoplatform.commons.api.notification.service.setting.UserSettingService;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.commons.utils.PropertyManager;
import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.component.ComponentRequestLifecycle;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

Expand All @@ -45,21 +49,27 @@
*/
public class EntityManagerService implements ComponentRequestLifecycle {

protected static Log log = ExoLogger.getLogger(EntityManagerService.class);

public static final String[] HIBERNATE_PROPERTIES = new String[] { "hibernate.show_sql",
"hibernate.format_sql",
"hibernate.use_sql_comments"};
private final static Log LOGGER = ExoLogger.getLogger(EntityManagerService.class);
private static final String PERSISTENCE_UNIT_NAME = "exo-pu";
private static EntityManagerFactory entityManagerFactory;
private ThreadLocal<EntityManager> instance = new ThreadLocal<>();

private ThreadLocal<EntityManager> instance = new ThreadLocal<EntityManager>();

private String persistenceName;

public EntityManagerService(InitParams params) {
ValueParam value = params.getValueParam("persistence.unit.name");
persistenceName = value.getValue();
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceName);
if (log.isInfoEnabled()) {
log.info("Created EntityManagerFactory instance: {}", persistenceName);
;
public EntityManagerService() {
// Get Hibernate properties in eXo global properties
final Properties properties = new Properties();
for (String propertyName : HIBERNATE_PROPERTIES) {
String propertyValue = PropertyManager.getProperty(propertyName);
if (StringUtils.isNotBlank(propertyValue)) {
properties.put(propertyName, propertyValue);
LOGGER.info("Setting [" + propertyName + "] to [" + propertyValue + "]");
}
}
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Created EntityManagerFactory instance: {}", PERSISTENCE_UNIT_NAME);
}
}

Expand Down Expand Up @@ -107,8 +117,8 @@ void closeEntityManager() {
}
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
if (log.isErrorEnabled()) {
log.error("Failed to commit transaction.", e);
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Failed to commit transaction.", e);
}

tx.rollback();
Expand All @@ -117,8 +127,8 @@ void closeEntityManager() {
em.close();
instance.set(null);

if (log.isDebugEnabled()) {
log.debug("Ended a request lifecycle of {} component service", EntityManagerService.class.getName());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Ended a request lifecycle of {} component service", EntityManagerService.class.getName());
}
}
}
Expand Down

0 comments on commit afb6917

Please sign in to comment.