Source Code Cross Referenced for BootstrapSystemUser.java in  » Project-Management » XPlanner-0.7b7 » com » technoetic » xplanner » security » install » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Project Management » XPlanner 0.7b7 » com.technoetic.xplanner.security.install 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.technoetic.xplanner.security.install;
002:
003:        import java.util.Date;
004:        import java.util.Iterator;
005:        import java.util.List;
006:
007:        import net.sf.hibernate.Hibernate;
008:        import net.sf.hibernate.HibernateException;
009:        import net.sf.hibernate.Session;
010:        import org.apache.log4j.Logger;
011:        import com.tacitknowledge.util.migration.MigrationContext;
012:        import com.tacitknowledge.util.migration.MigrationException;
013:
014:        import com.technoetic.xplanner.db.hibernate.GlobalSessionFactory;
015:        import com.technoetic.xplanner.db.hibernate.HibernateHelper;
016:        import com.technoetic.xplanner.db.hsqldb.HsqlServer;
017:        import com.technoetic.xplanner.domain.Person;
018:        import com.technoetic.xplanner.domain.Role;
019:        import com.technoetic.xplanner.domain.RoleAssociation;
020:        import com.technoetic.xplanner.security.auth.Permission;
021:
022:        public class BootstrapSystemUser {
023:            private Logger log = Logger.getLogger(getClass());
024:            protected static final String SYSADMIN_USER_ID = "sysadmin";
025:            protected static final int PATCH_LEVEL = 2;
026:            protected static final String PATCH_NAME = "XPlanner bootstrap";
027:
028:            public BootstrapSystemUser() {
029:                //      setLevel(new Integer(PATCH_LEVEL));
030:                //      setName(PATCH_NAME);
031:            }
032:
033:            public void run(String sysadminId) throws Exception {
034:                try {
035:                    HibernateHelper.initializeHibernate();
036:                    Session session = GlobalSessionFactory.get().openSession();
037:                    try {
038:                        List people = session.find("from person in class "
039:                                + Person.class.getName()
040:                                + " where person.userId = ?", sysadminId,
041:                                Hibernate.STRING);
042:                        Iterator personItr = people.iterator();
043:                        Person sysadmin;
044:                        if (personItr.hasNext()) {
045:                            sysadmin = (Person) personItr.next();
046:                            log.info("using " + sysadminId + " user");
047:                        } else {
048:                            sysadmin = createSysAdmin(sysadminId, session);
049:                        }
050:                        Role viewerRole = initializeRole(session, "viewer", 1,
051:                                8);
052:                        Role editorRole = initializeRole(session, "editor", 2,
053:                                7);
054:                        Role adminRole = initializeRole(session, "admin", 3, 6);
055:                        Role sysadminRole = initializeRole(session,
056:                                SYSADMIN_USER_ID, 4, 5);
057:                        addRoleAssociation(session, sysadminRole.getId(),
058:                                sysadmin.getId(), 0);
059:                        createPermission(session, sysadminRole, "%", "%");
060:                        createNegativePermission(session, editorRole,
061:                                "system.project", "create.project");
062:                        createNegativePermission(session, editorRole,
063:                                "system.person", "create.person");
064:                        createNegativePermission(session, adminRole,
065:                                "system.project", "create.project");
066:                        createPermission(session, adminRole, "%", "admin%");
067:                        createPermission(session, editorRole, "%", "create%");
068:                        createPermission(session, editorRole, "%", "edit%");
069:                        createPermission(session, editorRole, "%", "integrate%");
070:                        createPermission(session, editorRole, "%", "delete%");
071:                        createPermission(session, viewerRole, "%", "read%");
072:                        session.flush();
073:                        session.connection().commit();
074:                    } finally {
075:                        session.close();
076:                    }
077:                } catch (Exception e) {
078:                    e.printStackTrace();
079:                    throw e;
080:                }
081:            }
082:
083:            private Person createSysAdmin(String sysadminId, Session session)
084:                    throws HibernateException {
085:                Person sysadmin;
086:                log.info("creating " + sysadminId + " user");
087:                sysadmin = new Person();
088:                sysadmin.setUserId(sysadminId);
089:                sysadmin.setName(sysadminId);
090:                sysadmin.setInitials("SYS");
091:                sysadmin.setEmail("no@reply.com");
092:                sysadmin
093:                        .setPassword("1tGWp1Bdm02Sw4bD7/o0N2ao405Tf8kjxGBW/A=="); // password=admin
094:                sysadmin.setLastUpdateTime(new Date());
095:                session.save(sysadmin);
096:                return sysadmin;
097:            }
098:
099:            private void addRoleAssociation(Session session, int roleId,
100:                    int personId, int projectId) throws HibernateException {
101:                session.save(new RoleAssociation(projectId, personId, roleId));
102:            }
103:
104:            private void createPermission(Session session, Role sysadminRole,
105:                    String resourceType, String permissionName)
106:                    throws HibernateException {
107:                Permission permission = new Permission(resourceType, 0,
108:                        sysadminRole.getId(), permissionName);
109:                session.save(permission);
110:            }
111:
112:            private void createNegativePermission(Session session,
113:                    Role sysadminRole, String resourceType,
114:                    String permissionName) throws HibernateException {
115:                Permission permission = new Permission(resourceType, 0,
116:                        sysadminRole.getId(), permissionName);
117:                permission.setPositive(false);
118:                session.save(permission);
119:            }
120:
121:            private Role initializeRole(Session session, String roleName,
122:                    int left, int right) throws HibernateException {
123:                List roles = session.find("from role in class "
124:                        + Role.class.getName() + " where role.name = ?",
125:                        roleName, Hibernate.STRING);
126:                Role role;
127:                if (roles.size() == 0) {
128:                    log.info("creating role: " + roleName);
129:                    role = new Role(roleName);
130:                    role.setLeft(left);
131:                    role.setRight(right);
132:                    session.save(role);
133:                } else {
134:                    role = (Role) roles.get(0);
135:                }
136:                return role;
137:            }
138:
139:            public void migrate(MigrationContext context)
140:                    throws MigrationException {
141:                try {
142:                    run(SYSADMIN_USER_ID);
143:                } catch (Exception e) {
144:                    throw new MigrationException("error during migration", e);
145:                } finally {
146:                    HsqlServer.shutdown();
147:                }
148:            }
149:
150:            public static void main(String[] args) {
151:                String sysadminId = SYSADMIN_USER_ID;
152:                if (args.length == 1) {
153:                    sysadminId = args[0];
154:                }
155:                BootstrapSystemUser action = new BootstrapSystemUser();
156:                try {
157:                    action.run(sysadminId);
158:                } catch (Exception e) {
159:                    e.printStackTrace();
160:                } finally {
161:                    HsqlServer.shutdown();
162:                }
163:            }
164:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.