- /* $Id: SasClientFunction.java,v 1.3.2.3 2009/01/28 12:57:21 prathika Exp $ */
- /*
- * @(#)SasClientFunction.java
- * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- import java.applet.*;
- import java.awt.*;
- import java.util.*;
- import java.io.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SasClientFunction {
- // This help/debug window only has a text area and a close button.
- TextArea text = null;
- Button close = null;
- // We need a reference to our applet to get some data
- AdventSasDemo sasBrowser = null;
- // The fonts used
- Font fontb;
- Font font;
- SASClient sasclient;
- /**
- * The constructor takes the applet as an argument.
- * It sets up the window and calls initSnmp()
- */
- public SasClientFunction(AdventSasDemo app) {
- sasBrowser = (AdventSasDemo) app;
- initSnmp();
- } // end of constructor
- void initSnmp() {
- try {
- sasclient = new SASClient(sasBrowser, true);
- sasclient.start();
- }
- catch (Exception e) {
- System.out.println("Error : " + e.getMessage());
- }
- }
- /** If a sub-node is selected, we go down the MIB tree */
- void downTree(String child) {
- String dir;
- if(child.equals(".." + File.separator))
- dir = sasBrowser.getPrevDir(sasBrowser.currentDir.getText());
- else
- dir = new String(sasBrowser.currentDir.getText() + child + "/");
- if(dir.equals(""))dir = dir + "/";
- listFiles(dir);
- } // end of downTree()
- void println(String s) {
- sasBrowser.resultArea.setText(s);
- }
- /** For Demonstrating SAS functionalities */
- void saveFile(String saveText, String fileName)
- {
- try {
- byte data[] = new byte[saveText.length()];
- data = saveText.getBytes();
- if(sasclient != null) {
- sasclient.saveFile(getSelectedDirPath()+fileName,data);
- listFiles(sasBrowser.currentDir.getText());
- }
- else
- System.out.println("Not connected to sas");
- }
- catch (Exception e){
- System.out.println(e);
- e.printStackTrace();
- }
- }
- void appendFile(String appendText, String fileName)
- {
- byte data[] = new byte[appendText.length()];
- try {
- data = appendText.getBytes();
- if(sasclient != null) {
- sasclient.appendFile(getSelectedDirPath()+fileName,data);
- listFiles(sasBrowser.currentDir.getText());
- }
- else
- System.out.println("Not connected to sas");
- }
- catch (Exception e){
- System.out.println(e);
- e.printStackTrace();
- }
- }
- void deleteFile(String fileName)
- {
- if(fileName == null) {
- println("No file selected");
- return;
- }
- try {
- if(sasclient != null) {
- sasclient.deleteFile(getSelectedDirPath()+fileName);
- listFiles(sasBrowser.currentDir.getText());
- }
- else
- System.out.println("Not connected to sas");
- }
- catch (Exception e){
- System.out.println(e);
- e.printStackTrace();
- }
- }
- void createDir(String dirName)
- {
- try {
- if(sasclient != null)
- sasclient.createDir(getSelectedDirPath()+dirName);
- else
- {
- println("Not connected to SAS");
- return;
- }
- listFiles(sasBrowser.currentDir.getText());
- }
- catch (Exception e){
- System.out.println(e);
- e.printStackTrace();
- }
- }
- void deleteDir()
- {
- String dirName = sasBrowser.listDir.getSelectedItem();
- if(dirName == null) {
- println("No directory selected");
- return;
- }
- try {
- if(sasclient != null) {
- sasclient.deleteDir(getSelectedDirPath()+dirName);
- listFiles(sasBrowser.currentDir.getText());
- }
- else {
- println("Not connected to SAS");
- return;
- }
- }
- catch (Exception e){
- System.out.println(e);
- e.printStackTrace();
- }
- }
- // some constants for clientCall types
- static int LIST_DIR_REQ = 1;
- static int LIST_DIR_RESP = 2;
- static int GET_FILE_REQ = 3;
- static int GET_FILE_RESP = 4;
- void listFiles(String dir)
- {
- byte data[] = null;
- String listedFiles = null;
- String modDir;
- int i = dir.indexOf("|");
- if(i>0)modDir = dir.substring(i+1);
- else modDir = dir;
- try {
- ByteArrayOutputStream baO = new ByteArrayOutputStream();
- DataOutputStream dO = new DataOutputStream(baO);
- dO.writeInt(LIST_DIR_REQ);
- dO.writeUTF(modDir);
- data = baO.toByteArray();
- if(sasclient != null)
- data = sasclient.clientCall(data);
- else
- {
- println("Not connected to SAS server");
- return;
- }
- DataInputStream dI = new DataInputStream(new ByteArrayInputStream(data));
- int type = dI.readInt();
- if(type == LIST_DIR_RESP)
- listedFiles = dI.readUTF();
- }
- catch (Exception e) {
- System.out.println(e);
- e.printStackTrace();
- }
- if(listedFiles != null) {
- StringTokenizer toks = new StringTokenizer(listedFiles,"n");
- int num = sasBrowser.childList.getItemCount();
- if (num>0) sasBrowser.childList.removeAll();
- num = sasBrowser.listDir.getItemCount();
- if (num>0) sasBrowser.listDir.removeAll();
- //To handle netscape when loaded from classpath
- boolean netscapeRootDir = false;
- if(dir.length() == 4) {
- if(dir.indexOf("|/") > 0) netscapeRootDir = true;
- }
- if(!((dir.equals("/")) || (dir.equals("\")) || (netscapeRootDir)))
- sasBrowser.listDir.add(".." + File.separator);
- while(toks.hasMoreTokens()) {
- String file = toks.nextToken();
- if(file.indexOf("<DIR>") < 0)
- sasBrowser.childList.add(file);
- else {
- file = file.substring(5);
- sasBrowser.listDir.add(file);
- }
- }
- }
- else {
- sasBrowser.resultArea.setText("Cannot List the Directory");
- }
- sasBrowser.currentDir.setText(dir);
- }
- void getFile()
- {
- String file = new String(sasBrowser.currentDir.getText() + File.separator + sasBrowser.childList.getSelectedItem());
- byte[] data = null;
- String gotFile = null;
- try {
- ByteArrayOutputStream baO = new ByteArrayOutputStream();
- DataOutputStream dO = new DataOutputStream(baO);
- dO.writeInt(GET_FILE_REQ);
- dO.writeUTF(file);
- data = baO.toByteArray();
- if(sasclient != null)
- data = sasclient.clientCall(data);
- else
- {
- println("Not connected to sas");
- return;
- }
- DataInputStream dI = new DataInputStream(new ByteArrayInputStream(data));
- int type = dI.readInt();
- if(type == GET_FILE_RESP)
- gotFile = dI.readUTF();
- else
- {
- println("Not able to get the file");
- return;
- }
- FileOutputStream fos = new FileOutputStream(sasBrowser.childList.getSelectedItem() +".sas");
- dO = new DataOutputStream(fos);
- dO.writeBytes(gotFile);
- println("Successfully got the file");
- }
- catch (Exception e) {
- System.out.println(e);
- e.printStackTrace();
- }
- }
- String getSelectedDirPath() {
- int i = sasBrowser.currentDir.getText().indexOf("SASusers");
- if (i != -1) return sasBrowser.currentDir.getText().substring(i+8);
- else return "";
- }
- }