我正在使用 lucene 来存储一些数据的索引。以下代码检查 /home/username 中的目录是否存在,如果未找到,则从头开始构建索引,创建目录等。
public static final String INDEX_PATH = "/home/username/appname/lucene/index";
private void buildCompleteIndex(int organizationId) {
synchronized(mutex) {
File path = new File(INDEX_PATH + "/" + String.valueOf(organizationId));
if(!path.exists()) {
try {
Utils.deleteDirectory(path);
} catch (IOException e) {
throw new LuceneIndexException("Error rebuilding index directory.", e);
}
path.mkdirs();
}
List<Contact> contactList = contactDAO.findAll(organizationId, true);
if(contactList != null) {
for(Contact contact : contactList) {
add(contact);
}
}
}
}
//Getters
private IndexReader getIndexReader(boolean readOnly, int organizationId) {
try {
if(directory == null) {
File path = getFile(organizationId);
directory = FSDirectory.open(path);
if(!IndexReader.indexExists(directory)) {
buildCompleteIndex(organizationId);
}
}
return IndexReader.open(directory, readOnly);
} catch (CorruptIndexException e) {
buildCompleteIndex(organizationId);
} catch (IOException e) {
buildCompleteIndex(organizationId);
}
return null;
}
当我从 Eclipse 中的虚拟 tomcat 实例进行部署时,这一切在开发中都非常有用,但在生产服务器上失败了。
为什么我可以在开发模式下写入目录,但在应用程序部署在服务器上时却不能?我没有创建目录的权限吗?我正在使用 Ubuntu Server 12.10 和 Tomcat7。
如何在服务器上创建正确的文件夹和文件?
是否有我应该允许我的应用程序在服务器上写入的特定文件夹?它始终与我的开发箱上的主/用户文件夹一起使用,但在服务器上可能有所不同,因为在应用程序运行时用户实际上并未登录?
更新: 我检查了当前设置为 700 的文件夹的权限。这可能是问题吗?在生产服务器上将此文件夹设置为 666 或 777 是否安全?即使 /home/username 的用户名没有登录,这个文件夹是否可写?我知道 700 意味着所有者拥有完全访问权限,但这是否包括 tomcat 应用程序?
更新:我试图将 /home/username 的权限更改为 755,但同样的问题仍然存在。
显示尝试创建文件夹时生成的错误的堆栈跟踪。
java.io.IOException: Cannot create directory: /home/ryandlf/thinkbooked.com/lucene/contacts/1
at org.apache.lucene.store.NativeFSLock.obtain(NativeFSLockFactory.java:171)
at org.apache.lucene.store.Lock.obtain(Lock.java:72)
at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:1108)
at com.thinkbooked.search.LuceneContactSearchEngine.getIndexWriter(LuceneContactSearchEngine.java:321)
at com.thinkbooked.search.LuceneContactSearchEngine.add(LuceneContactSearchEngine.java:68)
at com.thinkbooked.search.LuceneContactSearchEngine.buildCompleteIndex(LuceneContactSearchEngine.java:285)
at com.thinkbooked.search.LuceneContactSearchEngine.getIndexReader(LuceneContactSearchEngine.java:303)
at com.thinkbooked.search.LuceneContactSearchEngine.find(LuceneContactSearchEngine.java:150)
at com.thinkbooked.search.LuceneContactSearchEngine.find(LuceneContactSearchEngine.java:145)
at com.thinkbooked.handlers.ClientListSearchHandler.init(ClientListSearchHandler.java:49)
at com.thinkbooked.event.EventListener.doPost(EventListener.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)