📄 raplaimportusers.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.examples;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.rapla.RaplaMainContainer;
import org.rapla.components.util.Tools;
import org.rapla.entities.Category;
import org.rapla.entities.User;
import org.rapla.facade.ClientFacade;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.framework.StartupEnvironment;
/**Demonstration for connecting your app and importing some users
*/
public class RaplaImportUsers {
public static void main(String[] args) {
if ( args.length< 1 ) {
System.out.println("Usage: filename");
System.out.println("Example: users.csv ");
return;
}
final ConsoleLogger logger = new ConsoleLogger( ConsoleLogger.LEVEL_INFO);
try
{
StartupEnvironment env = new SimpleConnectorStartupEnvironment( "localhost", 8051, "/",false, logger);
RaplaMainContainer container = new RaplaMainContainer( env);
importFile( container.getContext(), args[0] );
// cleanup the Container
container.dispose();
}
catch ( Exception e )
{
logger.error("Could not start test ", e );
}
}
private static void importFile(RaplaContext context,String filename) throws Exception {
System.out.println(" Please enter the admin password ");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String adminPass = stdin.readLine();
// get an interface to the facade and login
ClientFacade facade = (ClientFacade)context.lookup(ClientFacade.ROLE);
if ( !facade.login("admin", adminPass.toCharArray() ) ) {
throw new RaplaException("Can't login");
}
importUsers( facade, new FileReader( filename ));
facade.logout();
}
public static void importUsers(ClientFacade facade, Reader reader) throws RaplaException, IOException {
String[][] entries = Tools.csvRead( reader, 5 );
Category rootCategory = facade.getUserGroupsCategory();
for ( int i=0;i<entries.length; i++ ) {
String[] lineEntries = entries[i];
String name = lineEntries[0];
String email = lineEntries[1];
String username = lineEntries[2];
String groupKey = lineEntries[3];
String password = lineEntries[4];
User user = facade.newUser();
user.setUsername( username );
user.setName ( name );
user.setEmail( email );
Category group = findCategory( rootCategory, groupKey );
if (group != null) {
user.addGroup( group );
}
facade.store(user);
facade.changePassword( user, new char[] {} ,password.toCharArray());
System.out.println("Imported user " + user + " with password '" + password + "'");
}
}
static private Category findCategory( Category rootCategory, String groupPath) {
Category group = rootCategory;
String[] groupKeys = Tools.split( groupPath, '/');
for ( int i=0;i<groupKeys.length; i++) {
group = group.getCategory( groupKeys[i] );
}
return group;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -