Permalink
Browse files

Formatting according to infinispan conventions

  • Loading branch information...
1 parent d130f9d commit ca44c233d4120ad5f254124f25883721f6fef2b4 @mgencur committed Apr 24, 2012
@@ -32,149 +32,148 @@
*/
public class InitializeCache implements SystemEventListener {
- private static final int USER_COUNT = 100;
-
- private static final int SEVEN_DAYS_IN_MILLISECONDS = 7 * 24 * 3600 * 1000;
- Random randomNumber = new Random();
-
- private Logger log = Logger.getLogger(this.getClass().getName());
-
- private CacheContainerProvider provider;
-
- private UserTransaction utx;
-
- @Override
- public void processEvent(SystemEvent event) throws AbortProcessingException {
- provider = getContextualInstance(getBeanManagerFromJNDI(),
- CacheContainerProvider.class);
- startup();
- }
-
- public void startup() {
- BasicCache<String, Object> users = provider.getCacheContainer().getCache("userCache");
- BasicCache<TweetKey, Object> tweets = provider.getCacheContainer().getCache("tweetCache");
-
- utx = getUserTransactionFromJNDI();
-
- try {
- utx.begin();
- for (int i = 1; i != USER_COUNT; i++) {
- User u = new User("user" + i, "Name" + i, "Surname" + i, "tmpPasswd", "Description of person " + i );
- String encryptedPass = hashPassword("pass" + i);
- u.setPassword(encryptedPass);
-
- //GENERATE 5 TWEETS FOR EACH USER
- for (int j = 1; j != 5; j++) {
- long randomTime = getRandomTime();
- Tweet t = new Tweet(u.getUsername(), "Tweet number " + j + " for user " + u.getName() + " at " + new Date(randomTime), randomTime );
- //store the tweet in a cache
- tweets.put(t.getKey(), t);
- u.getTweets().add(t.getKey());
- }
- //store the user in a cache
- users.put(u.getUsername(), u);
+ private static final int USER_COUNT = 100;
+
+ private static final int SEVEN_DAYS_IN_MILLISECONDS = 7 * 24 * 3600 * 1000;
+ Random randomNumber = new Random();
+
+ private Logger log = Logger.getLogger(this.getClass().getName());
+
+ private CacheContainerProvider provider;
+
+ private UserTransaction utx;
+
+ @Override
+ public void processEvent(SystemEvent event) throws AbortProcessingException {
+ provider = getContextualInstance(getBeanManagerFromJNDI(), CacheContainerProvider.class);
+ startup();
+ }
+
+ public void startup() {
+ BasicCache<String, Object> users = provider.getCacheContainer().getCache("userCache");
+ BasicCache<TweetKey, Object> tweets = provider.getCacheContainer().getCache("tweetCache");
+
+ utx = getUserTransactionFromJNDI();
+
+ try {
+ utx.begin();
+ for (int i = 1; i != USER_COUNT; i++) {
+ User u = new User("user" + i, "Name" + i, "Surname" + i, "tmpPasswd",
+ "Description of person " + i);
+ String encryptedPass = hashPassword("pass" + i);
+ u.setPassword(encryptedPass);
+
+ // GENERATE 5 TWEETS FOR EACH USER
+ for (int j = 1; j != 5; j++) {
+ long randomTime = getRandomTime();
+ Tweet t = new Tweet(u.getUsername(), "Tweet number " + j + " for user "
+ + u.getName() + " at " + new Date(randomTime), randomTime);
+ // store the tweet in a cache
+ tweets.put(t.getKey(), t);
+ u.getTweets().add(t.getKey());
}
-
- //GENERATE 10 RANDOM FOLLOWERS AND FOLLOWINGS FOR EACH USER
- for (int i = 1; i != USER_COUNT; i++) {
- User u = (User) users.get("user" + i);
- for (User watcher : generateRandomUsers(u, 10, USER_COUNT)) {
- u.getWatchers().add(watcher.getUsername());
- }
- for (User watching : generateRandomUsers(u, 30, USER_COUNT)) {
- u.getWatching().add(watching.getUsername());
- }
- users.replace("user" + i, u);
+ // store the user in a cache
+ users.put(u.getUsername(), u);
+ }
+
+ // GENERATE 10 RANDOM FOLLOWERS AND FOLLOWINGS FOR EACH USER
+ for (int i = 1; i != USER_COUNT; i++) {
+ User u = (User) users.get("user" + i);
+ for (User watcher : generateRandomUsers(u, 10, USER_COUNT)) {
+ u.getWatchers().add(watcher.getUsername());
}
-
- utx.commit();
- log.info("Successfully imported data!");
- } catch (Exception e) {
- log.warning("An exception occured while populating the datagrid! Rolling back the transaction");
- e.printStackTrace();
- if (utx != null) {
- try {
- utx.rollback();
- } catch (Exception e1) {
- }
+ for (User watching : generateRandomUsers(u, 30, USER_COUNT)) {
+ u.getWatching().add(watching.getUsername());
}
- }
- }
-
- private long getRandomTime() {
- //get random time at most 7 days old
- return Calendar.getInstance().getTimeInMillis() - randomNumber.nextInt(SEVEN_DAYS_IN_MILLISECONDS);
- }
-
- private Set<User> generateRandomUsers(User forWhom, int count, int outOf) {
- BasicCache<String, Object> users = provider.getCacheContainer().getCache("userCache");
- Random r = new Random(getRandomTime());
- Set<User> result = new HashSet<User>();
- while (result.size() != count) {
- int id = (r.nextInt(outOf - 1)) + 1; //do not return 0
- User u = (User) users.get("user" + id);
- if (u != null && u.equals(forWhom)) continue;
- result.add(u);
- }
- return result;
- }
-
- public static String hashPassword(String password) {
- String hashword = null;
- try {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- md5.update(password.getBytes());
- BigInteger hash = new BigInteger(1, md5.digest());
- hashword = hash.toString(16);
- } catch (NoSuchAlgorithmException nsae) {
- throw new RuntimeException("No MD5 algorithm found for password encryption!");
- }
- return hashword;
- }
-
- private BeanManager getBeanManagerFromJNDI() {
- InitialContext context;
- Object result;
- try {
- context = new InitialContext();
- result = context.lookup("java:comp/BeanManager");
- } catch (NamingException e) {
- throw new RuntimeException(
- "BeanManager could not be found in JNDI", e);
- }
- return (BeanManager) result;
- }
-
- private UserTransaction getUserTransactionFromJNDI() {
- InitialContext context;
- Object result;
- try {
- context = new InitialContext();
- result = context.lookup("java:comp/UserTransaction");
- } catch (NamingException ex) {
- throw new RuntimeException(
- "UserTransaction could not be found in JNDI", ex);
- }
- return (UserTransaction) result;
- }
-
- @SuppressWarnings("unchecked")
- public <T> T getContextualInstance(final BeanManager manager,
- final Class<T> type) {
- T result = null;
- Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
- if (bean != null) {
- CreationalContext<T> context = manager
- .createCreationalContext(bean);
- if (context != null) {
- result = (T) manager.getReference(bean, type, context);
- }
- }
- return result;
- }
-
- @Override
- public boolean isListenerForSource(Object source) {
- return source instanceof Application;
- }
+ users.replace("user" + i, u);
+ }
+
+ utx.commit();
+ log.info("Successfully imported data!");
+ } catch (Exception e) {
+ log.warning("An exception occured while populating the datagrid! Rolling back the transaction");
+ e.printStackTrace();
+ if (utx != null) {
+ try {
+ utx.rollback();
+ } catch (Exception e1) {
+ }
+ }
+ }
+ }
+
+ private long getRandomTime() {
+ // get random time at most 7 days old
+ return Calendar.getInstance().getTimeInMillis()
+ - randomNumber.nextInt(SEVEN_DAYS_IN_MILLISECONDS);
+ }
+
+ private Set<User> generateRandomUsers(User forWhom, int count, int outOf) {
+ BasicCache<String, Object> users = provider.getCacheContainer().getCache("userCache");
+ Random r = new Random(getRandomTime());
+ Set<User> result = new HashSet<User>();
+ while (result.size() != count) {
+ int id = (r.nextInt(outOf - 1)) + 1; // do not return 0
+ User u = (User) users.get("user" + id);
+ if (u != null && u.equals(forWhom))
+ continue;
+ result.add(u);
+ }
+ return result;
+ }
+
+ public static String hashPassword(String password) {
+ String hashword = null;
+ try {
+ MessageDigest md5 = MessageDigest.getInstance("MD5");
+ md5.update(password.getBytes());
+ BigInteger hash = new BigInteger(1, md5.digest());
+ hashword = hash.toString(16);
+ } catch (NoSuchAlgorithmException nsae) {
+ throw new RuntimeException("No MD5 algorithm found for password encryption!");
+ }
+ return hashword;
+ }
+
+ private BeanManager getBeanManagerFromJNDI() {
+ InitialContext context;
+ Object result;
+ try {
+ context = new InitialContext();
+ result = context.lookup("java:comp/BeanManager");
+ } catch (NamingException e) {
+ throw new RuntimeException("BeanManager could not be found in JNDI", e);
+ }
+ return (BeanManager) result;
+ }
+
+ private UserTransaction getUserTransactionFromJNDI() {
+ InitialContext context;
+ Object result;
+ try {
+ context = new InitialContext();
+ result = context.lookup("java:comp/UserTransaction");
+ } catch (NamingException ex) {
+ throw new RuntimeException("UserTransaction could not be found in JNDI", ex);
+ }
+ return (UserTransaction) result;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T getContextualInstance(final BeanManager manager, final Class<T> type) {
+ T result = null;
+ Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
+ if (bean != null) {
+ CreationalContext<T> context = manager.createCreationalContext(bean);
+ if (context != null) {
+ result = (T) manager.getReference(bean, type, context);
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public boolean isListenerForSource(Object source) {
+ return source instanceof Application;
+ }
}
@@ -4,40 +4,40 @@
import java.util.Calendar;
public class Tweet implements Serializable {
-
- private static final long serialVersionUID = -3268797376991935010L;
-
- private TweetKey key;
- private String message;
-
- public Tweet(String username, String message) {
- this.message = message;
- this.key = new TweetKey(username, Calendar.getInstance().getTimeInMillis());
- }
-
- //for application initilization purposes
- public Tweet(String username, String message, long timestamp) {
- this.message = message;
- this.key = new TweetKey(username, timestamp);
- }
-
- public TweetKey getKey() {
- return key;
- }
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- public String getOwner() {
- return this.key.getOwner();
- }
-
- public long getTimeOfPost() {
- return this.key.getTimeOfPost();
- }
+
+ private static final long serialVersionUID = -3268797376991935010L;
+
+ private TweetKey key;
+ private String message;
+
+ public Tweet(String username, String message) {
+ this.message = message;
+ this.key = new TweetKey(username, Calendar.getInstance().getTimeInMillis());
+ }
+
+ // for application initilization purposes
+ public Tweet(String username, String message, long timestamp) {
+ this.message = message;
+ this.key = new TweetKey(username, timestamp);
+ }
+
+ public TweetKey getKey() {
+ return key;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public String getOwner() {
+ return this.key.getOwner();
+ }
+
+ public long getTimeOfPost() {
+ return this.key.getTimeOfPost();
+ }
}
Oops, something went wrong.

0 comments on commit ca44c23

Please sign in to comment.