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.AbstractModule;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import com.google.inject.TypeLiteral; 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.multibindings.Multibinder;
import com.google.inject.spi.InjectionListener; import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.TypeEncounter; 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); Multibinder<AbstractPlugin> pluginBinder = Multibinder.newSetBinder(binder(), AbstractPlugin.class);
pluginBinder.addBinding().to(pluginClass).in(Singleton.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 @Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { 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 @Override
public void afterInjection(I injectee) { public void afterInjection(I injectee) {
AbstractPlugin plugin = (AbstractPlugin) injectee; AbstractPlugin plugin = (AbstractPlugin) injectee;
plugin.setId(pluginId); plugin.setId(pluginId);
plugin.setName(pluginName); plugin.setName(pluginName);
plugin.setVendor(pluginVendor); plugin.setVendor(pluginVendor);
plugin.setVersion(pluginVersion); plugin.setVersion(pluginVersion);
plugin.setDescription(pluginDescription); plugin.setDescription(pluginDescription);
plugin.setDependencyIds(pluginDependencies); plugin.setDependencyIds(pluginDependencies);
} }
}); });
}
} }
}); });

View File

@ -30,7 +30,7 @@ public interface GeneralDao {
* @throws a * @throws a
* HibernateException if no matching entity is found * 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 * 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 @Transactional
@Override @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); return (T) getSession().load(unproxy(entityClass), entityId);
} }
@ -61,7 +61,7 @@ public class GeneralDaoImpl implements GeneralDao {
@Transactional @Transactional
@Override @Override
public <T extends AbstractEntity> void deleteById(Class<T> entityClass, Long entityId) { 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); delete(entity);
} }

View File

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

View File

@ -3,16 +3,18 @@ package com.pmease.commons.product;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; 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 { public class HelloResource {
@GET @GET
@RequiresPermissions("view") @RequiresPermissions("write")
public String whoami() { 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.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -138,10 +140,13 @@ public class Product extends AbstractPlugin {
@Override @Override
public void postStartDependents() { public void postStartDependents() {
User user = new User(); DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
if (generalDaoProvider.get().find(User.class, 1L) == null) { criteria.add(Restrictions.eq("loginName", "admin"));
user.setLoginName("robin"); if (generalDaoProvider.get().find(criteria) == null) {
user.setPasswordHash(passwordService.encryptPassword("robin")); User user = new User();
user.setLoginName("admin");
user.setPasswordHash(passwordService.encryptPassword("12345"));
user.setFullName("Administrator");
generalDaoProvider.get().save(user); generalDaoProvider.get().save(user);
} }

View File

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

View File

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

View File

@ -13,8 +13,6 @@ public class WicketConfig extends AbstractWicketConfig {
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
mountPage("/test", HomePage.class);
} }
@Override @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.markup.html.link.Link;
import org.apache.wicket.model.AbstractReadOnlyModel; import org.apache.wicket.model.AbstractReadOnlyModel;
import com.pmease.commons.product.model.User;
import com.pmease.commons.security.SecurityHelper; import com.pmease.commons.security.SecurityHelper;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -20,8 +21,7 @@ public class HomePage extends WebPage {
@Override @Override
public String getObject() { public String getObject() {
SecurityHelper.getSubject().checkPermission("admin"); return SecurityHelper.getUserDisplayName(User.class, "Guest");
return SecurityHelper.getSubject().getPrincipal().toString();
} }
})); }));
@ -30,7 +30,7 @@ public class HomePage extends WebPage {
@Override @Override
public void onClick() { 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 @Override
public void onClick() { 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. # database settings below and comment out settings for other databases.
#---------------------------------------------------------------------------------- #----------------------------------------------------------------------------------
#hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.dialect=org.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class=org.hsqldb.jdbcDriver hibernate.connection.driver_class=org.hsqldb.jdbcDriver
#hibernate.connection.url=jdbc:hsqldb:file:${installDir}/sampledb/@dbName@;shutdown=true hibernate.connection.url=jdbc:hsqldb:file:${installDir}/sampledb/@dbName@;shutdown=true
#hibernate.connection.username=sa hibernate.connection.username=sa
#hibernate.connection.password= hibernate.connection.password=
#---------------------------------------------------------------------------------- #----------------------------------------------------------------------------------
@ -24,11 +24,11 @@
# sure all tables are of innodb type. # sure all tables are of innodb type.
#---------------------------------------------------------------------------------- #----------------------------------------------------------------------------------
hibernate.dialect=org.hibernate.dialect.MySQLDialect #hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver #hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/@dbName@ #hibernate.connection.url=jdbc:mysql://localhost:3306/@dbName@
hibernate.connection.username=root #hibernate.connection.username=root
hibernate.connection.password=root #hibernate.connection.password=root
#---------------------------------------------------------------------------------- #----------------------------------------------------------------------------------

View File

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