putlistresource.java
来自「很棒的web服务器源代码」· Java 代码 · 共 769 行 · 第 1/2 页
JAVA
769 行
/** * Get the file to use to store the edited list of published files. * @return The file. */ public File getPubFile() { File file = getFile(); if (file != null) return new File(file+".pub"); else return null; } /** * Get the file to use to store the edited list of deleted files. * @return The file. */ public File getDelFile() { File file = getFile(); if (file != null) return new File(file+".del"); else return null; } /** * Get the root directory of the public server to update. * @return The root directory of the public server space, supposed to * be controled by CVS. */ public File getRoot() { return (File) getValue(ATTR_ROOT, null); } /** * Get this user's local CVS space root directory. * @return The usre's root of the CVS local space, assumed to be * under CVS control. */ public File getCvsSpace() { return (File) getValue(ATTR_SPACE, null); } protected synchronized void write(File file, Enumeration enum) { if (file == null) return; File backup = null; // Save old version if available: if ( file.exists() ) { backup = new File(file+".bak"); if ( backup.exists() ) backup.delete(); file.renameTo(backup); } try { Vector v = new Vector(10); while (enum.hasMoreElements()) v.addElement(enum.nextElement()); AttributeHolder holders[] = new AttributeHolder[v.size()]; v.copyInto(holders); Writer writer = new BufferedWriter(new FileWriter(file)); serializer.writeResources(holders, writer); } catch (Exception ex) { ex.printStackTrace(); if ( backup != null ) backup.renameTo(file); } } /** * Dump the current list of edited files back to disk. */ protected synchronized void writeList() { write(getFile(), entries.elements()); } /** * Dump the current list of published files back to disk. */ protected synchronized void writePubList() { write(getPubFile(), published.elements()); } /** * Dump the current list of [ublished files back to disk. */ protected synchronized void writeDelList() { write(getDelFile(), getDelEntries()); } protected synchronized void removeUnconfirmedDelEntries() { Enumeration enum = getDelEntries(); while (enum.hasMoreElements()) { DeletedEntry e = (DeletedEntry) enum.nextElement(); if (! e.isConfirmed()) removeDelEntry(e.getKey()); } } /** * Restore the list from the file. */ protected synchronized void readList() { File file = getFile(); try { Reader reader = new BufferedReader(new FileReader(file)); AttributeHolder holders[] = serializer.readAttributeHolders(reader); for (int i = 0 ; i < holders.length ; i++) addEntry((PutedEntry)holders[i]); } catch (Exception ex) { // FIXME ex.printStackTrace(); } } /** * Restore the published list from the file. */ protected synchronized void readPubList() { File file = getPubFile(); try { Reader reader = new BufferedReader(new FileReader(file)); AttributeHolder holders[] = serializer.readAttributeHolders(reader); for (int i = 0 ; i < holders.length ; i++) addPubEntry((PutedEntry)holders[i]); } catch (Exception ex) { // FIXME ex.printStackTrace(); } } /** * Restore the deleted list from the file. */ protected synchronized void readDelList() { File file = getDelFile(); try { Reader reader = new BufferedReader(new FileReader(file)); AttributeHolder holders[] = serializer.readAttributeHolders(reader); for (int i = 0 ; i < holders.length ; i++) { DeletedEntry e = (DeletedEntry) holders[i]; e.confirm(); addDelEntry(e); } } catch (Exception ex) { // FIXME ex.printStackTrace(); } } protected PutedEntry lookupEntry(Request request) { ResourceReference rr = request.getTargetResource(); String k = request.getURL().toExternalForm(); Resource r = null; if (rr != null) { try { r = rr.lock(); if ( r instanceof FileResource ) k = ((FileResource) r).getFile().getAbsolutePath().toString(); } catch (InvalidResourceException ex) { // continue } finally { rr.unlock(); } } return (PutedEntry) entries.get(k); } protected DeletedEntry lookupDelEntry(Request request) { ResourceReference rr = request.getTargetResource(); String k = request.getURL().toExternalForm(); Resource r = null; if (rr != null) { try { r = rr.lock(); if ( r instanceof FileResource ) k = ((FileResource) r).getFile().getAbsolutePath().toString(); } catch (InvalidResourceException ex) { // continue } finally { rr.unlock(); } } return (DeletedEntry) dentries.get(k); } /** * Register the given request, which must has a PUT method. * @param file The modified file. */ public synchronized int registerRequest(Request request) { PutedEntry e = lookupEntry(request); if ( e == null ) { e = PutedEntry.makeEntry(request); addEntry(e); } else { e.update(request); } if (getAutoPublishFlag()) { return publish(e); } else { return FILE_UC; } } public synchronized void registerDeleteRequest(Request request) { DeletedEntry e = (DeletedEntry) DeletedEntry.makeEntry(request); addDelEntry(e); } public synchronized int confirmDelete(Request request) { DeletedEntry e = lookupDelEntry(request); if (e != null) { e.confirm(); if (getAutoDeleteFlag()) return delete(e); } return FILE_UC; } /** * Delete the file relative to the given entry. * @param entry The DeletedEntry. * @return FILE_UC, FILE_DEL */ protected int delete(DeletedEntry de) { File file = new File(de.getFilename()); File sfile = getServerFile(file); if (sfile.exists()) { if (debug) System.out.println("Deleting : "+sfile); sfile.delete(); removeDelEntry(de.getKey()); return FILE_DEL; } else { if (debug) System.out.println("Nothing to delete : "+sfile); removeDelEntry(de.getKey()); return FILE_UC; } } /** * Publish the file relative to the given entry. * @param entry The PutedEntry. * @return FILE_UC, FILE_CF, FILE_PB, FILE_MG */ protected int publish(PutedEntry pe) { File file = new File(pe.getFilename()); File sfile = getServerFile(file); int status = FILE_UC; try { // First step: does the private version needs commit ? File d = new File(file.getParent()); CvsDirectory c = CvsDirectory.getManager(d, props); if ( c.status(file.getName()) == CVS.FILE_M ) { String author = pe.getAuthor(); String env [] = { "USER="+author , "LOGNAME="+author }; String msg = ((author != null) ? "Published by "+author+" through Jigsaw" : "Published through Jigsaw"); c.commit(file.getName(), msg, env); } else if ( debug ) { System.out.println("PutList: no commit needed on "+ file.getAbsolutePath()+ " st="+c.status(file.getName())); } // Second step: publish File sd = new File(sfile.getParent()); try { CvsDirectory sc = CvsDirectory.getManager(sd, props); String filename = sfile.getName(); int cvs_status = sc.status(filename); if (debug) { System.out.println("publishing "+ CvsDirectory.statusToString(cvs_status)+ " file : "+filename); } if (cvs_status == CVS.FILE_C) { //conflict! we try to merge //create a backup file File backup = new File(sfile.getParent(), filename+".bak"); try { org.w3c.util.IO.copy(sfile, backup); //try to merge sc.update(filename); cvs_status = sc.status(filename); if (cvs_status == CVS.FILE_M) { //merge done, so commit. String author = pe.getAuthor(); String env [] = { "USER="+author , "LOGNAME="+author }; String msg = ((author != null) ? "Merged by "+author+ " through Jigsaw" : "Merged through Jigsaw"); sc.commit(filename, msg, env); //done so delete backup file backup.delete(); status = FILE_MG; } else if (cvs_status == CVS.FILE_C) { //merge failed sfile.delete(); backup.renameTo(sfile); status = FILE_CF; } } catch (IOException ex) { ex.printStackTrace(); status = FILE_CF; } } else if (cvs_status != CVS.FILE_OK) { sc.update(filename); status = FILE_PB; } else if ( debug ) { System.out.println("PutList: no update needed on "+ sfile.getAbsolutePath()+ " st="+ CvsDirectory.statusToString(cvs_status)); } } catch (UncheckedOutException ex) { // perform a get from root File root = new File(getRoot().getAbsolutePath()); CvsDirectory sc = CvsDirectory.getManager(root, props); String fpath = file.getAbsolutePath(); String fspace = getCvsSpace().getAbsolutePath(); String path = fpath.substring(fspace.length()+1); sc.get(path); status = FILE_PB; } // Last step: remove published entries: entries.remove(pe.getKey()); // publication time pe.setValue(PutedEntry.ATTR_TIME, new Long(System.currentTimeMillis())); addPubEntry(pe); } catch (CvsException ex) { ex.printStackTrace(); } return status; } public synchronized void notifyUnload() { writeList(); writePubList(); writeDelList(); super.notifyUnload(); } public void initialize(Object values[]) { super.initialize(values); // Prepare empty entry list: File file = getFile(); if ((file != null) && file.exists()) readList(); File pub = getPubFile(); if ((pub != null) && pub.exists()) readPubList(); File del = getDelFile(); if ((del != null) && del.exists()) readDelList(); // Get the server properties: this.props = getServer().getProperties(); try { registerFrameIfNone("org.w3c.jigedit.filters.PutListFrame", "putlist-frame"); } catch (Exception ex) { ex.printStackTrace(); } } public PutListResource() { super(); this.entries = new Hashtable(11); this.dentries = new Hashtable(11); this.published = new Hashtable(11); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?