Refactor commons.product

This commit is contained in:
robin shine 2013-06-29 14:32:34 +08:00
parent 411ee06c15
commit acc2e79211
12 changed files with 59 additions and 48 deletions

View File

@ -6,7 +6,7 @@ import java.util.Set;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.matcher.AbstractMatcher;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.TypeEncounter;
@ -34,26 +34,31 @@ public abstract class AbstractPluginModule extends AbstractModule implements Dep
Multibinder<AbstractPlugin> pluginBinder = Multibinder.newSetBinder(binder(), AbstractPlugin.class);
pluginBinder.addBinding().to(pluginClass).in(Singleton.class);
bindListener(Matchers.any(), new TypeListener() {
bindListener(new AbstractMatcher<TypeLiteral<?>>() {
@Override
public boolean matches(TypeLiteral<?> t) {
return t.getRawType() == pluginClass;
}
}, new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
if (pluginClass == type.getRawType()) {
encounter.register(new InjectionListener<I>() {
encounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(I injectee) {
AbstractPlugin plugin = (AbstractPlugin) injectee;
plugin.setId(pluginId);
plugin.setName(pluginName);
plugin.setVendor(pluginVendor);
plugin.setVersion(pluginVersion);
plugin.setDescription(pluginDescription);
plugin.setDependencyIds(pluginDependencies);
}
});
}
@Override
public void afterInjection(I injectee) {
AbstractPlugin plugin = (AbstractPlugin) injectee;
plugin.setId(pluginId);
plugin.setName(pluginName);
plugin.setVendor(pluginVendor);
plugin.setVersion(pluginVersion);
plugin.setDescription(pluginDescription);
plugin.setDependencyIds(pluginDependencies);
}
});
}
});

View File

@ -30,7 +30,7 @@ public interface GeneralDao {
* @throws a
* HibernateException if no matching entity is found
*/
<T extends AbstractEntity> T getReference(Class<T> entityClass, Long entityId);
<T extends AbstractEntity> T load(Class<T> entityClass, Long entityId);
/**
* If the id of the entity is null or zero, add it to the datastore and

View File

@ -38,7 +38,7 @@ public class GeneralDaoImpl implements GeneralDao {
@Transactional
@Override
public <T extends AbstractEntity> T getReference(Class<T> entityClass, Long entityId) {
public <T extends AbstractEntity> T load(Class<T> entityClass, Long entityId) {
return (T) getSession().load(unproxy(entityClass), entityId);
}
@ -61,7 +61,7 @@ public class GeneralDaoImpl implements GeneralDao {
@Transactional
@Override
public <T extends AbstractEntity> void deleteById(Class<T> entityClass, Long entityId) {
T entity = getReference(unproxy(entityClass), entityId);
T entity = load(unproxy(entityClass), entityId);
delete(entity);
}

View File

@ -31,7 +31,7 @@ public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
@Override
public T getReference(Long entityId) {
return generalDao.getReference(entityClass, entityId);
return generalDao.load(entityClass, entityId);
}
@Override

View File

@ -3,16 +3,18 @@ package com.pmease.commons.product;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
@Path("/whoami")
import com.pmease.commons.product.model.User;
import com.pmease.commons.security.SecurityHelper;
@Path("/hello")
public class HelloResource {
@GET
@RequiresPermissions("view")
@RequiresPermissions("write")
public String whoami() {
return "user: " + SecurityUtils.getSubject().getPrincipal();
return "Hello " + SecurityHelper.getUserDisplayName(User.class, "Guest");
}
}

View File

@ -16,6 +16,8 @@ import org.eclipse.jetty.server.ssl.SslSocketConnector;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -138,10 +140,13 @@ public class Product extends AbstractPlugin {
@Override
public void postStartDependents() {
User user = new User();
if (generalDaoProvider.get().find(User.class, 1L) == null) {
user.setLoginName("robin");
user.setPasswordHash(passwordService.encryptPassword("robin"));
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
criteria.add(Restrictions.eq("loginName", "admin"));
if (generalDaoProvider.get().find(criteria) == null) {
User user = new User();
user.setLoginName("admin");
user.setPasswordHash(passwordService.encryptPassword("12345"));
user.setFullName("Administrator");
generalDaoProvider.get().save(user);
}

View File

@ -14,7 +14,7 @@ import com.pmease.commons.web.AbstractWicketConfig;
public class ProductModule extends AbstractPluginModule {
@Override
@Override
protected void configure() {
super.configure();

View File

@ -24,7 +24,7 @@ public class UserRealm extends AbstractRealm<User> {
@Override
protected Collection<String> doGetPermissions(Long userId) {
if (userId == 0L)
return Arrays.asList("view");
return Arrays.asList("read");
else
return Arrays.asList("*");
}

View File

@ -13,8 +13,6 @@ public class WicketConfig extends AbstractWicketConfig {
@Override
protected void init() {
super.init();
mountPage("/test", HomePage.class);
}
@Override

View File

@ -7,6 +7,7 @@ import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.AbstractReadOnlyModel;
import com.pmease.commons.product.model.User;
import com.pmease.commons.security.SecurityHelper;
@SuppressWarnings("serial")
@ -20,8 +21,7 @@ public class HomePage extends WebPage {
@Override
public String getObject() {
SecurityHelper.getSubject().checkPermission("admin");
return SecurityHelper.getSubject().getPrincipal().toString();
return SecurityHelper.getUserDisplayName(User.class, "Guest");
}
}));
@ -30,7 +30,7 @@ public class HomePage extends WebPage {
@Override
public void onClick() {
SecurityUtils.getSubject().login(new UsernamePasswordToken("robin", "robin"));
SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "12345"));
}
});
@ -48,10 +48,11 @@ public class HomePage extends WebPage {
@Override
public void onClick() {
SecurityUtils.getSubject().checkPermission("bird");
SecurityUtils.getSubject().checkPermission("write");
}
});
}
}

View File

@ -3,11 +3,11 @@
# database settings below and comment out settings for other databases.
#----------------------------------------------------------------------------------
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class=org.hsqldb.jdbcDriver
#hibernate.connection.url=jdbc:hsqldb:file:${installDir}/sampledb/@dbName@;shutdown=true
#hibernate.connection.username=sa
#hibernate.connection.password=
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:file:${installDir}/sampledb/@dbName@;shutdown=true
hibernate.connection.username=sa
hibernate.connection.password=
#----------------------------------------------------------------------------------
@ -24,11 +24,11 @@
# sure all tables are of innodb type.
#----------------------------------------------------------------------------------
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/@dbName@
hibernate.connection.username=root
hibernate.connection.password=root
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate.connection.driver_class=com.mysql.jdbc.Driver
#hibernate.connection.url=jdbc:mysql://localhost:3306/@dbName@
#hibernate.connection.username=root
#hibernate.connection.password=root
#----------------------------------------------------------------------------------

View File

@ -21,7 +21,7 @@ public class SecurityHelper extends SecurityUtils {
Preconditions.checkNotNull(principal);
Long userId = (Long) principal;
if (userId != 0L)
return (T) AppLoader.getInstance(GeneralDao.class).getReference(userClass, userId);
return (T) AppLoader.getInstance(GeneralDao.class).load(userClass, userId);
else
return null;
}