我想與使用此類的服務器進行連接。該類獲取在HashMap中對圖像執行操作所需的參數列表。然後在doInBackground中,我逐個執行Image上所需的操作。對於這OVFImage部署中的一個類的代碼粘貼也低於試圖動態地實例化類
public class ImageDeployer extends SwingWorker<Boolean,String> {
public ImageDeployer(){
}
public ImageDeployer(HashMap<String, String> volIDMap, HashMap<String, String> osMap) {
// TODO Auto-generated constructor stub
this.volIDMap = volIDMap;
this.osMap = osMap;
System.out.println(volIDMap);
System.out.println(osMap);
makeAConnection();
try {
doInBackground();
System.out.println("Do In Background");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void makeAConnection(){
inputFile = RESTEngine.getFilePath();
Properties defaultProps = new Properties();
try {
fin = new FileInputStream(inputFile);
defaultProps.load(fin);
fin.close();
}
catch(FileNotFoundException e1){
System.out.println("The properties file supposed to contain Authorization parameters was not found.");
e1.printStackTrace();
System.exit(-1);
}
catch(IOException e1){
System.out.println("An exception occured while trying to open the properties file");
e1.printStackTrace();
System.exit(-1);
}
// assign variables from Input file with default value as null
user = defaultProps.getProperty("UserID", null);
host = defaultProps.getProperty("PowerVC_IP_ADDRESS", null);
password = defaultProps.getProperty("UserPass" ,null);
jsch = new JSch();
try {
session = jsch.getSession(user, host, 22);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel=session.openChannel("exec");
channel.setInputStream(null);
try {
in = channel.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Connection Successful");
} catch (JSchException e) {
// TODO Auto-generated catch block
System.out.println("Unable to connect");
e.printStackTrace();
}
}
@Override
protected Boolean doInBackground() throws Exception {
ImageDeployer imageDeployer = new ImageDeployer();
imageDeployer.makeAConnection();
for(String imageName : volIDMap.keySet()){
String volID = volIDMap.get(imageName);
String oS = osMap.get(imageName);
if (oS.equalsIgnoreCase("aix")){
imageDeployer = new OVFImageDeployer(volID, oS, imageName);
}
// Other Cases depending upon the OS Type
}
return null;
}
}
代碼爲OVFImage部署
public class OVFImageDeployer extends PowerVCImageDeployer {
public OVFImageDeployer(String VolID,String oS,String imageName){
String command="/usr/bin/powervc-devtools/powervc-devcli glance image-create json "+imageName+" "+oS+" "+VolID;
try {
((ChannelExec)channel).setCommand(command);
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
現在,當我運行代碼我得到的線((ChannelExec)channel).setCommand(command)
一個NullPointerException
。 我知道如果我在OVFImageDeployer
的try塊後面加上makeAConnection
這段代碼就可以工作,但是我不想一次又一次地建立連接。我想要一個連接初始化一次,所有的操作只能使用該連接來執行。
你的'channel'屬性在哪裏定義?它在'PowerVCImageDeployer'中嗎? –
是的,該頻道是頻道類型的變量,是一個公共變量。我已確定它不是特定於方法的,並將其聲明爲 public class ImageDeployer extends SwingWorker。 –
,並且當'ImageDeployer'的實例被構造並且'ImageDeployer'實例上的所有方法調用應該使用相同的'channel'時,你想'channel'被初始化?我對麼? –