Java tutorial
/* * Created 2007/07/29 * Copyright (C) 2003-2007 Naoki Iwami (naoki@limy.org) * * This file is part of limyweb-lrd. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.limy.lrd.admin; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.filefilter.FileFileFilter; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.limy.common.svn.CommitFileInfo; import org.limy.common.web.WebworkUtils; import org.limy.lrd.common.LrdException; import org.limy.lrd.common.LrdRepository; import org.limy.lrd.common.RepositoryBean; import org.limy.lrd.common.RepositoryBean.RepositoryType; import org.limy.lrd.common.bean.LrdDeployInfo; import org.limy.lrd.common.bean.LrdNode; import org.limy.lrd.common.bean.LrdDeployInfo.DeployType; import org.limy.lrd.common.util.LrdUtils; import org.tmatesoft.svn.core.SVNErrorCode; import org.tmatesoft.svn.core.SVNException; /** * ???? * @author Naoki Iwami */ public class AddRepositoryAction extends AbstractAdminAction { /** logger */ private static final Log LOG = LogFactory.getLog(AddRepositoryAction.class); /** ?ID */ private static final String DEFAULT_DEPLOY_ID = "LOCAL"; // ------------------------ Fields /** ?ID */ private String repositoryId; /** ??? */ private String repositoryName; /** ? */ private int repositoryType; /** ?URL */ private int repositoryUrlType; /** ?URL */ private String repositoryUrl; /** ?URLSVN */ private String repositorySvnUrl; /** SVN?? */ private String repositorySvnUser; /** SVN */ private String repositorySvnPassword; /** */ private String localContextDir; // ------------------------ Override Methods @Override protected String doExecuteAdmin() { RepositoryBean bean = new RepositoryBean(); bean.setId(repositoryId); bean.setName(repositoryName); if (StringUtils.isEmpty(localContextDir)) { bean.setLocalContentFile(LrdUtils.getDefaultLocalContentFile(repositoryId)); } else { bean.setLocalContentFile(new File(localContextDir)); } if (repositoryType == 1) { // SVN bean.setType(RepositoryType.SVN); if (!repositorySvnUrl.endsWith("/")) { repositorySvnUrl += "/"; } bean.setUrl(repositorySvnUrl); bean.setUserName(repositorySvnUser); bean.setPassword(repositorySvnPassword); } else { // FILE bean.setType(RepositoryType.FILE); if (repositoryUrlType == 0) { // bean.setUrl(new File(WebworkUtils.getFile("repository"), repositoryId).getAbsolutePath()); } else { // bean.setUrl(repositoryUrl); } } getconfigRootBean().addRepository(bean); getRepositoryCreator().refreshRepositoryBeans(); try { createEmpryContents(bean); } catch (LrdException e) { LOG.warn(e.getMessage(), e); return ERROR; } catch (IOException e) { LOG.warn(e.getMessage(), e); return ERROR; } updateConfigRootBean(); return SUCCESS; } // ------------------------ Web Getter Methods /** * ?ID???? * @return ?ID */ public String getRepositoryId() { return repositoryId; } /** * ?ID??? * @param repositoryId ?ID */ public void setRepositoryId(String repositoryId) { this.repositoryId = repositoryId; } /** * ??????? * @return ??? */ public String getRepositoryName() { return repositoryName; } /** * ?????? * @param repositoryName ??? */ public void setRepositoryName(String repositoryName) { this.repositoryName = repositoryName; } /** * ????? * @return ? */ public int getRepositoryType() { return repositoryType; } /** * ???? * @param repositoryType ? */ public void setRepositoryType(int repositoryType) { this.repositoryType = repositoryType; } /** * ?URL???? * @return ?URL */ public int getRepositoryUrlType() { return repositoryUrlType; } /** * ?URL??? * @param repositoryUrlType ?URL */ public void setRepositoryUrlType(int repositoryUrlType) { this.repositoryUrlType = repositoryUrlType; } /** * ?URL???? * @return ?URL */ public String getRepositoryUrl() { return repositoryUrl; } /** * ?URL??? * @param repositoryUrl ?URL */ public void setRepositoryUrl(String repositoryUrl) { this.repositoryUrl = repositoryUrl; } /** * ?URLSVN???? * @return ?URLSVN */ public String getRepositorySvnUrl() { return repositorySvnUrl; } /** * ?URLSVN??? * @param repositorySvnUrl ?URLSVN */ public void setRepositorySvnUrl(String repositorySvnUrl) { this.repositorySvnUrl = repositorySvnUrl; } /** * SVN?????? * @return SVN?? */ public String getRepositorySvnUser() { return repositorySvnUser; } /** * SVN????? * @param repositorySvnUser SVN?? */ public void setRepositorySvnUser(String repositorySvnUser) { this.repositorySvnUser = repositorySvnUser; } /** * SVN???? * @return SVN */ public String getRepositorySvnPassword() { return repositorySvnPassword; } /** * SVN??? * @param repositorySvnPassword SVN */ public void setRepositorySvnPassword(String repositorySvnPassword) { this.repositorySvnPassword = repositorySvnPassword; } /** * ???? * @return */ public String getLocalContextDir() { return localContextDir; } /** * ??? * @param localContextDir */ public void setLocalContextDir(String localContextDir) { this.localContextDir = localContextDir; } // ------------------------ Private Methods private void createEmpryContents(RepositoryBean bean) throws LrdException, IOException { if (bean.getType() == RepositoryType.FILE) { initLocalRepository(bean); } if (bean.getType() == RepositoryType.SVN) { initSvnRepository(bean); } copyDefaultTemplateFiles(bean); saveDefaultTemplateFiles(bean); // ?Root????? createInitFiles(bean); // ? LrdDeployInfo deployInfo = new LrdDeployInfo(DeployType.LOCAL, DEFAULT_DEPLOY_ID, LrdUtils.getLocalContentUrl(repositoryId)); deployInfo.setName(DEFAULT_DEPLOY_ID); bean.addDeployInfo(deployInfo); } /** * ????? * @param bean ?Baan * @throws LrdException * @throws IOException I/O */ private void initLocalRepository(RepositoryBean bean) throws LrdException, IOException { File baseDir = new File(bean.getUrl()); if (!baseDir.exists()) { // ????????? baseDir.mkdirs(); // createInitFiles(bean); } } /** * SVN????? * @param bean ?Baan * @throws LrdException * @throws IOException */ private void initSvnRepository(RepositoryBean bean) throws LrdException, IOException { LrdRepository repository = bean.getRepository(); try { // ???? repository.getDirectoryRoot(); } catch (LrdException e) { if (e.getCause() instanceof SVNException) { // ????? SVNException se = (SVNException) e.getCause(); SVNErrorCode errorCode = se.getErrorMessage().getErrorCode(); if (SVNErrorCode.FS_NOT_FOUND == errorCode) { // ????????? createEmptySvnRepository(bean); return; } if (SVNErrorCode.RA_SVN_REPOS_NOT_FOUND == errorCode) { // ????? // TODO svnadmin ????????????? } // TODO ?????? throw e; } } } /** * SVN?????? * @param bean ?Baan * @throws LrdException */ private void createEmptySvnRepository(RepositoryBean bean) throws LrdException { LrdRepository repository = bean.getRepository(); repository.addDirectory(""); } /** * ????? * @param bean * @throws LrdException * @throws IOException */ private void copyDefaultTemplateFiles(RepositoryBean bean) throws LrdException, IOException { File baseDir = bean.getLocalContentFile(); // ???????? LrdRepository repository = bean.getRepository(); LrdNode root = repository.getDirectoryRoot(); LrdNode node = root.searchSubNode(".template"); if (node != null) { Collection<String> lrdPaths = new ArrayList<String>(); for (LrdNode child : node.getSubNodes()) { for (LrdNode file : child.getSubNodes()) { lrdPaths.add(file.getPath().getRelativePath()); } } CommitFileInfo[] files = repository.getRepositoryFiles(lrdPaths.toArray(new String[lrdPaths.size()])); for (CommitFileInfo file : files) { FileUtils.writeByteArrayToFile(new File(baseDir, file.getPath()), file.getContents()); } return; } for (File file : WebworkUtils.getFile("template/vm").listFiles((FileFilter) FileFileFilter.FILE)) { FileUtils.copyFile(file, new File(baseDir, ".template/vm/" + file.getName())); } for (File file : WebworkUtils.getFile("template/style").listFiles((FileFilter) FileFileFilter.FILE)) { FileUtils.copyFile(file, new File(baseDir, ".template/style/" + file.getName())); } } /** * ?????? * @param bean * @throws IOException */ private void saveDefaultTemplateFiles(RepositoryBean bean) throws IOException { LrdRepository repository = bean.getRepository(); Collection<CommitFileInfo> commitInfos = new ArrayList<CommitFileInfo>(); for (File file : WebworkUtils.getFile("template/vm").listFiles((FileFilter) FileFileFilter.FILE)) { commitInfos.add(new CommitFileInfo(".template/vm/" + file.getName(), FileUtils.readFileToByteArray(file), null)); } for (File file : WebworkUtils.getFile("template/style").listFiles((FileFilter) FileFileFilter.FILE)) { commitInfos.add(new CommitFileInfo(".template/style/" + file.getName(), FileUtils.readFileToByteArray(file), null)); } try { repository.addAndcommitMultiFile(commitInfos.toArray(new CommitFileInfo[commitInfos.size()])); } catch (LrdException e) { // ?????? LOG.error(e.getMessage(), e); } } /** * ?????? * @param bean * @throws LrdException * @throws IOException I/O */ private void createInitFiles(RepositoryBean bean) throws LrdException, IOException { LrdNode root = bean.getRepository().getDirectoryRoot(); if (root.searchSubNode("index.lrd") == null) { // index.lrd ???? getModel().createFile(repositoryId, "index.lrd"); String contents = IOUtils.toString(new FileInputStream(WebworkUtils.getFile("WEB-INF/cnf/empty.lrd"))); getModel().commit(repositoryId, "index.lrd", contents); // new LrdDwrExt().deployFull(bean.getId(), DEFAULT_DEPLOY_ID); } } }