Java tutorial
package com.web.server.util; /*Copyright 2013 - 2015, Arun_Soundararajan (arun_srajan_2007@yahoo.com).and/or its affiliates. All files in this repository or distribution are licensed under the Apache License, Version 2.0 (the "License"); you may not use any files in this repository or distribution except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.nio.file.Files; import java.nio.file.Paths; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FilenameUtils; import org.jgroups.JChannel; import org.jgroups.Message; import org.jgroups.ReceiverAdapter; import org.jgroups.View; import org.jgroups.Message.Flag; import org.jgroups.protocols.UDP; import org.jgroups.protocols.pbcast.NAKACK; import org.jgroups.stack.ProtocolStack; import org.jgroups.util.Util; import static java.nio.file.StandardCopyOption.*; public class FarmWarFileTransfer extends ReceiverAdapter implements FarmWarFileTransferMBean { JChannel channel; FileOutputStream fout; final List<String> state = new LinkedList<String>(); String transfer = "Transfer:"; String transferEnd = "TransferEnd:"; String deployDir = ""; String farmwarDir = ""; String address; String clusterGroup; static FarmWarFileTransfer farmWarFileTransfer = null; private FarmWarFileTransfer(String deployDir, String farmwarDir, String clusterGroup) { this.deployDir = deployDir; this.farmwarDir = farmwarDir; this.clusterGroup = clusterGroup; ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); FarmWarDirWatcher task = new FarmWarDirWatcher(farmwarDir, this); exec.scheduleAtFixedRate(task, 0, 1000, TimeUnit.MILLISECONDS); } public static FarmWarFileTransfer getInstance(String deployDir, String farmwarDir, String clusterGroup) { if (farmWarFileTransfer == null) { farmWarFileTransfer = new FarmWarFileTransfer(deployDir, farmwarDir, clusterGroup); return farmWarFileTransfer; } return farmWarFileTransfer; } public void viewAccepted(View new_view) { System.out.println("** view: " + new_view); } public void receive(Message msg) { //String line=msg.getSrc() + ": " + msg.getObject(); //System.out.println(msg.getSrc().toString()+" "+address); //if(!msg.getSrc().toString().equals(address)){ byte[] buffer = msg.getBuffer(); String str = new String(buffer); if (str.startsWith(transfer)) { fout = null; try { fout = new FileOutputStream(deployDir + "/" + str.substring(transfer.length())); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (str.startsWith(transferEnd)) { try { System.out.println("file closed"); fout.close(); //Files.move(Paths.get(farmwarDir, str.substring(transferEnd.length())),Paths.get(deployDir, str.substring(transferEnd.length())),REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } else { try { //System.out.println("writing buffer"+buffer.length+" "+msg.getObject()); fout.write(buffer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //} /*System.out.println(line); synchronized(state) { state.add(line); }*/ } public void getState(OutputStream output) throws Exception { synchronized (state) { Util.objectToStream(state, new DataOutputStream(output)); } } public void setState(InputStream input) throws Exception { List<String> list = (List<String>) Util.objectFromStream(new DataInputStream(input)); synchronized (state) { state.clear(); state.addAll(list); } //System.out.println("received state (" + list.size() + " messages in chat history):"); for (String str : list) { System.out.println(str); } } public void sendFilesToClusterServer(File file) { try { if (file.isFile() && file.exists()) { int len; byte[] bt = new byte[1048576]; Message msg = new Message(); msg.setFlag(Flag.RSVP); msg.setBuffer(("Transfer:" + file.getName()).getBytes()); channel.send(msg); FileInputStream str = new FileInputStream(file.getAbsolutePath()); int count = 0; while ((len = str.read(bt)) > 0) { msg = new Message(); msg.setFlag(Flag.RSVP); //msg.setObject(""+count++); msg.setBuffer(bt, 0, len); channel.send(msg); } msg = new Message(); msg.setBuffer(("TransferEnd:" + file.getName()).getBytes()); msg.setFlag(Flag.RSVP); channel.send(msg); str.close(); System.out.println(file.getName() + " transferred"); } } catch (Exception ex) { System.out.println("File could not be transferred" + file.getName()); } } public void start() throws Exception { channel = new JChannel(); channel.setReceiver(this); /*ProtocolStack stack=new ProtocolStack(); // 2 channel.setProtocolStack(stack); stack.addProtocol(new UDP().setValue("bind_addr", InetAddress.getByName("0.0.0.0"))); //stack.addProtocol(new NAKACK()); stack.init();*/ channel.connect(clusterGroup); address = channel.getAddress().toString(); channel.getState(null, 10000); // channel.close(); } }