package com.zensar.dao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.zensar.bean.Users;
import com.zensar.util.Dom;
/**
* <p>
* Title: 业务逻辑处理类
* </p>
*
* @author
* @version 1.2,2008-2-20
* @since svn 1.0
*/
public class BussImpl implements Bussiness
{
/**
* 查询所有用户信息
*
* @return
*/
public List findUsers()
{
List<Users> list = new ArrayList<Users>();
Users user = null;
BufferedReader br = null;
String copyTemp = null;
try
{
File file = new File(Dom.filepath + "passwd");
br = new BufferedReader(new FileReader(file));
int i = 0;
while ((copyTemp = br.readLine()) != null)
{
String a = copyTemp.trim();
if ("[users]".equals(a))
i = 1;
if (i == 1 && a.indexOf("#") != 0 && a.length() > 0
&& !"[users]".equals(a))
{
user = new Users();
int a1 = a.indexOf("=");
String uname = a.substring(0, a1).trim();
String passw = a.substring(a1 + 1, a.length()).trim();
String rights = this.findRights(uname);
user.setUsername(uname);
user.setPassword(passw);
user.setRights(rights);
list.add(user);
}
}
br.close();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return list;
}
/**
* 查询用户密码
*
* @param username
* @return
*/
public String findRights(String username)
{
BufferedReader br = null;
String rights = null;
String copyTemp = null;
try
{
File file = new File(Dom.filepath + "authz");
br = new BufferedReader(new FileReader(file));
int i = 0;
while ((copyTemp = br.readLine()) != null)
{
String a = copyTemp.trim();
if ("[groups]".equals(a))
i = 1;
if (i == 1 && a.indexOf("#") != 0 && a.length() > 0
&& !"[groups]".equals(a))
{
int a1 = a.indexOf("=");
// 未做必须存在=号的判断
String uname = a.substring(0, a1).trim();
if (username.equals(uname))
{
rights = a.substring(a1 + 1, a.length()).trim();
}
}
}
br.close();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return rights;
}
/**
* 新增用户
*
* @param user
*/
public void addUsers(Users user)
{
BufferedReader br = null;
PrintWriter out = null;
String copyTemp = null;
try
{
br = new BufferedReader(new FileReader(Dom.filepath + "passwd"));
out = new PrintWriter(new FileWriter(Dom.filepath + "passwdtemp"));
int i = 0;
while ((copyTemp = br.readLine()) != null)
{
String a = copyTemp.trim();
if ("[users]".equals(a))
i = 1;
// 指定区域 指定非注释 非空白行 非第第1行关键字
if (i == 1 && !"[users]".equals(a))
{
out
.println(user.getUsername() + " = "
+ user.getPassword());
i = 2;
out.println(copyTemp);
} else
{
out.println(copyTemp);
}
out.flush();
}
out.close();
br.close();
// 处理权限
if (user.getRights() != null)
{
br = new BufferedReader(new FileReader(Dom.filepath + "authz"));
out = new PrintWriter(
new FileWriter(Dom.filepath + "authztemp"));
int j = 0;
while ((copyTemp = br.readLine()) != null)
{
String a = copyTemp.trim();
if ("[groups]".equals(a))
j = 1;
if (j == 1 && !"[groups]".equals(a))
{
out.println(user.getUsername() + " = "
+ user.getRights());
j = 2;
out.println(copyTemp);
} else
{
out.println(copyTemp);
}
out.flush();
}
out.close();
br.close();
}
copy(Dom.filepath + "passwdtemp", Dom.filepath + "passwd");
if (user.getRights() != null)
{
copy(Dom.filepath + "authztemp", Dom.filepath + "authz");
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
if (out != null)
{
out.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 删除用户
*
* @param unames
*/
public void delUsers(String[] unames)
{
BufferedReader br = null;
PrintWriter out = null;
String copyTemp = null;
try
{
br = new BufferedReader(new FileReader(Dom.filepath + "passwd"));
out = new PrintWriter(new FileWriter(Dom.filepath + "passwdtemp"));
int i = 0;
while ((copyTemp = br.readLine()) != null)
{
String a = copyTemp.trim();
if ("[users]".equals(a))
i = 1;
if (i == 1 && a.indexOf("#") != 0 && a.length() > 0
&& !"[users]".equals(a))
{
int a1 = a.indexOf("=");
// 未做必须存在=号的判断
String uname = a.substring(0, a1).trim();
if (!ifEqual(uname, unames))
{
out.println(copyTemp);
}
} else
{
out.println(copyTemp);
}
out.flush();
}
out.close();
br.close();
br = new BufferedReader(new FileReader(Dom.filepath + "authz"));
out = new PrintWriter(new FileWriter(Dom.filepath + "authztemp"));
int j = 0;
while ((copyTemp = br.readLine()) != null)
{
String a = copyTemp.trim();
if ("[groups]".equals(a))
j = 1;
if (j == 1 && a.indexOf("#") != 0 && a.length() > 0
&& !"[groups]".equals(a))
{
int a1 = a.indexOf("=");
// 未做必须存在=号的判断
String uname = a.substring(0, a1).trim();
if (!ifEqual(uname, unames))
{
out.println(copyTemp);
}
} else
{
out.println(copyTemp);
}
out.flush();
}
out.close();
br.close();
this.copy(Dom.filepath + "passwdtemp", Dom.filepath + "passwd");
this.copy(Dom.filepath + "authztemp", Dom.filepath + "authz");
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
if (out != null)
{
out.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 判断用户名是否匹配
*
* @param uname
* @param unames
* @return
*/
public boolean ifEqual(String uname, String[] unames)
{
boolean a = false;
for (int i = 0; i < unames.length; i++)
{
if (uname.equals(unames[i]))
a = true;
}
return a;
}
/**
* 拷贝文件
*
* @param form
* @param to
*/
public void copy(String form, String to)
{
File fileform = new File(form);
File fileto = new File(to);
FileInputStream in = null;
FileOutputStream out = null;
try
{
in = new FileInputStream(fileform);
out = new FileOutputStream(fileto);
byte[] buff = new byte[1024];
int len = 0;
// 判断读的文件是否还有内容 没有内容时 返回为-1
while ((len = in.read(buff)) != -1)
{
out.write(buff, 0, len);
}
in.close();
out.close();
fileform.delete();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 查询用户名字
* @param username
* @return
*/
public Users fidUserName(String username)
{
Users dto
- 1
- 2
前往页