import java.io.IOException; import java.io.Serializable; import java.security.Principal; import java.util.Random; import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.servlet.ServletException; import org.apache.commons.lang3.RandomStringUtils; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.jackrabbit.api.security.principal.JackrabbitPrincipal; import org.apache.jackrabbit.api.security.user.Group; import org.apache.jackrabbit.api.security.user.UserManager; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.jcr.api.SlingRepository; import org.apache.sling.jcr.base.util.AccessControlUtil; import com.day.cq.commons.jcr.JcrConstants; /** This program will create random users and groups and assign members to groups in random manner. You can run this program for different counter HOW TO USE: (This will run for default group and user count and default counter) HOST:PORT/bin/test/grouptest OR (This will run for specified group and default user count and default counter) HOST:PORT/bin/test/grouptest.html&groupcount=<Some Number> OR (This will run for specified user and default group count and default counter) HOST:PORT/bin/test/grouptest.html&usercount =<Some Number> OR (This will run for default user and default group count and specified counter) HOST:PORT/bin/test/grouptest.html&counter =<Some Number> OR HOST:PORT/bin/test/grouptest.html&debug=true/false OR ANY COMBINATION OF ABOVE @author: yogesh upadhyay */ @Component(metatype = true, label = "Test Group Membership", description = "Servlet to Test effect of number of member in group. Accepted param usercount,groupcount,counter") @Service @Properties({ @Property(name = "sling.servlet.paths", value = "/bin/test/grouptest")}) public class GroupMembershipSimulate extends SlingAllMethodsServlet { private static final long serialVersionUID = 1L; @Reference private SlingRepository repository; private static final String NN_PROFILE = "profile"; private static int USER_COUNT = 20000; private static int GROUP_COUNT = 4; @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { Session session = null; boolean isDebug = false; long startTime;
try { session = repository.loginAdministrative(null); UserManager userManager = AccessControlUtil.getUserManager(session); if(request.getParameter("usercount")!=null){ USER_COUNT = Integer.parseInt(request.getParameter("usercount")); } if(request.getParameter("groupcount")!=null){ GROUP_COUNT = Integer.parseInt(request.getParameter("groupcount")); } if(request.getParameter("debug")!=null){ isDebug = Boolean.getBoolean(request.getParameter("debug")); }
String[] somegroups = new String[GROUP_COUNT]; for(int i=0;i<GROUP_COUNT;i++){ somegroups[i]="test"+i; } /** * If counter is present just update property and return */ if(request.getParameter("counter")!=null){
int counter= Integer.parseInt(request.getParameter("counter")); response.getWriter().println("Counter is "+counter); int saveCounter=0; for(int i=0;i<counter;i++){ for(String s:somegroups){ if(userManager.getAuthorizable(s)!=null){ String groupNodePath = userManager.getAuthorizable(s).getPath(); if(session.nodeExists(groupNodePath)){ Node node = session.getNode(groupNodePath); Node profileNode; if(node.hasNode(NN_PROFILE)){ profileNode = node.getNode(NN_PROFILE); profileNode.setProperty("timeNow", System.currentTimeMillis()); }else{ profileNode = node.addNode(NN_PROFILE); profileNode.setPrimaryType(JcrConstants.NT_UNSTRUCTURED); profileNode.setProperty("timeNow", System.currentTimeMillis()); } } }
} } if(session!=null){ session.save(); response.getWriter().println("All Profile updated successfully"); session.logout(); }
return;
} /** * else create random user and group */ for(String s:somegroups){ if(null==userManager.getAuthorizable(s)){ response.getWriter().println("Creating Group "+ s); Principal principal = new PrincipalImpl(s); startTime = System.currentTimeMillis(); userManager.createGroup(principal,"/home/groups/test"); //Or you can use this to create group /** userManager.createGroup(new Principal(){ public String getName() { return s; } }); */ if(isDebug){ response.getWriter().println("Time taken to create group "+s +" is "+(System.currentTimeMillis()-startTime) +" ms");
} } } Random rand = new Random(); for(int i=0;i<USER_COUNT;i++){ String randomUid = RandomStringUtils.randomAlphabetic(10); int groupIndex = rand.nextInt(GROUP_COUNT); if(userManager.getAuthorizable(randomUid)==null){ response.getWriter().println("Creating User "+ randomUid); startTime = System.currentTimeMillis(); userManager.createUser(randomUid, randomUid,new PrincipalImpl(randomUid),"/home/users/test"); if(isDebug){ response.getWriter().println("Time taken to create user "+randomUid +" is "+(System.currentTimeMillis()-startTime) +" ms");
} Group group = (Group)(userManager.getAuthorizable(somegroups[groupIndex])); startTime = System.currentTimeMillis(); group.addMember(userManager.getAuthorizable(randomUid)); if(isDebug){ response.getWriter().println("Time taken to addmember "+randomUid + " to group "+group.getID() +" is "+(System.currentTimeMillis()-startTime) +" ms"); } } } } catch (RepositoryException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(session!=null){ session.logout();
} } }
/** * Base class for implementations of <code>JackrabbitPrincipal</code>. */ private class PrincipalImpl implements JackrabbitPrincipal, Serializable { /** the serial number */ private static final long serialVersionUID = 384040549033267804L; /** * the name of this principal */ private final String name; /** * Creates a new principal with the given name. * * @param name the name of this principal */ public PrincipalImpl(String name) { if (name == null || name.length() == 0) { throw new IllegalArgumentException("Principal name can neither be null nor empty String."); } this.name = name; } //----------------------------------------------------------< Principal >--- /** * {@inheritDoc} */ public String getName() { return name; } //-------------------------------------------------------------< Object >--- /** * Two principals are equal, if their names are. * @see Object#equals(Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof JackrabbitPrincipal) { return name.equals(((Principal) obj).getName()); } return false; } /** * @return the hash code of the principals name. * @see Object#hashCode() */ @Override public int hashCode() { return name.hashCode(); } /** * @see Object#toString() */ @Override public String toString() { return getClass().getName() + ":" + name; } } } |