/** * */ package vn.mobileid.fms.client; import java.util.concurrent.Callable; import javax.jcr.ItemNotFoundException; import javax.jcr.LoginException; import javax.jcr.Node; import javax.jcr.Property; import javax.jcr.RepositoryException; import javax.jcr.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; // TODO: Auto-generated Javadoc /** * The Class JCRDowloadTask. * * @author ThaoTruong * @created Oct 8, 2018 */ public class JCRDowloadTask implements Callable{ /** The Constant LOGGER. */ static final Logger LOGGER = LoggerFactory.getLogger(JCRDowloadTask.class); /** The jcr manager. */ private JCRManager jcrManager; /** The uuid. */ private String uuid; /** * Instantiates a new JCR dowload task. * * @param jcrManager the jcr manager * @param uuid the uuid */ public JCRDowloadTask(JCRManager jcrManager, String uuid) { // TODO Auto-generated constructor stub this.jcrManager = jcrManager; this.uuid = uuid; } /* (non-Javadoc) * @see java.util.concurrent.Callable#call() */ @Override public JCRFile call() throws Exception { // TODO Auto-generated method stub int retryCnt = 0; do { try { retryCnt++; return downloadJCR(); // } catch (JCRException e) { // // TODO: handle exception // throw e; } catch (Exception e) { LOGGER.error("Retry/max: {}/{}. Fail to download file with uuid {}, caused by {}", retryCnt, jcrManager.retryDownload, uuid, e.getMessage()); if (retryCnt > jcrManager.retryDownload || e.getMessage().contains("Invalid UUID:") || e instanceof ItemNotFoundException) { throw e; } } } while (true); } /** * Download JCR. * * @return the JCR file * @throws LoginException the login exception * @throws ItemNotFoundException the item not found exception * @throws RepositoryException the repository exception * @throws JCRException */ public JCRFile downloadJCR() throws Exception { Thread thread = Thread.currentThread(); String tName = thread.getName(); JCRFile file = new JCRFile(); //Session session = jcrManager.getSession(tName); //--> connect 1 time to server Session session = jcrManager.getSession(); Node obj; try { obj = session.getNodeByIdentifier(uuid); //--> connect 1 time to server }catch (ItemNotFoundException e) { // TODO: handle exception LOGGER.warn("==> FAIL to get file with uuid {}, caused by {}, refresh to load", uuid, e.getMessage()); session.refresh(true); try { obj = session.getNodeByIdentifier(uuid); }catch (RepositoryException ex) { // TODO: handle exception LOGGER.warn("==> FAIL to get file with uuid {}, caused by {}, sleep for sync and switch to another session", uuid, e.getMessage()); Thread.sleep(jcrManager.timeoutSync * 1000); session = jcrManager.getSession(); obj = session.getNodeByIdentifier(uuid); } }catch (RepositoryException e) { // TODO: handle exception if(e.getMessage().contains("Invalid UUID:")) { throw e; } LOGGER.warn("==> FAIL to get file with uuid {}, caused by {}, sleep for sync and switch to another session", uuid, e.getMessage()); Thread.sleep(jcrManager.timeoutSync * 1000); session = jcrManager.getSession(); obj = session.getNodeByIdentifier(uuid); } file.setUuid(obj.getProperty(Property.JCR_UUID).getString()); file.setMimeType(obj.getProperty(Property.JCR_MIMETYPE).getString()); file.setLastModified(obj.getProperty(Property.JCR_LAST_MODIFIED).getString()); file.setLastModifiedBy(obj.getProperty(Property.JCR_LAST_MODIFIED_BY).getString()); file.setStream(obj.getProperty(Property.JCR_DATA).getBinary().getStream()); //--> connect 1 time to server String path = obj.getPath(); String[] paths = path.split("/"); file.setFolderName(paths[1]); file.setFileName(paths[2]); file.setFilePath(path.substring(0, path.lastIndexOf("/"))); return file; } }