01: package ru.emdev.EmForge.svn;
02:
03: import org.apache.commons.lang.StringUtils;
04: import org.apache.commons.logging.Log;
05: import org.apache.commons.logging.LogFactory;
06: import org.springframework.context.ApplicationContext;
07: import org.springframework.context.ApplicationContextAware;
08: import org.tmatesoft.svn.core.SVNException;
09: import org.tmatesoft.svn.core.io.SVNRepository;
10:
11: import ru.emdev.EmForge.security.EmForgeAnonymousProcessingFilter;
12:
13: /**
14: * This is application-wide wrapper for Subversion Repository It allow us to use request-based UserSvnRepositoryFactory
15: * in the application-wide beans
16: */
17: public class SvnRepositoryFactoryWrapper implements
18: ApplicationContextAware, SvnRepositoryFactory {
19:
20: protected final Log logger = LogFactory.getLog(getClass());
21: ApplicationContext m_appContext;
22:
23: /** Path to the repository */
24: private String m_repositoryPath;
25:
26: private EmForgeAnonymousProcessingFilter m_anonymousProvider;
27:
28: /**
29: * @param i_repositoryPath
30: */
31: public void setRepositoryPath(String i_repositoryPath) {
32:
33: m_repositoryPath = i_repositoryPath;
34: }
35:
36: /**
37: * @param i_anonymousProvider
38: */
39: public void setAnonymousProvider(
40: EmForgeAnonymousProcessingFilter i_anonymousProvider) {
41:
42: m_anonymousProvider = i_anonymousProvider;
43: }
44:
45: /**
46: * @see ru.emdev.EmForge.svn.SvnRepositoryFactory#isHasRepository()
47: */
48: public Boolean isHasRepository() {
49:
50: return StringUtils.isEmpty(m_repositoryPath);
51: }
52:
53: /**
54: * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
55: */
56: public void setApplicationContext(ApplicationContext i_appContext) {
57:
58: m_appContext = i_appContext;
59: }
60:
61: /**
62: * Gets repository for configured path and for current user
63: *
64: * @note Do not store repository you get from this method in your beans. Lifetime for this repository object is
65: * request and managed by special bean
66: * @see ru.emdev.EmForge.svn.SvnRepositoryFactory#getSvnRepository()
67: */
68: public SVNRepository getSvnRepository() throws SVNException {
69:
70: // get repository factory
71: try {
72: SVNRepository svnRepository = (SVNRepository) m_appContext
73: .getBean("svnRepository");
74:
75: return svnRepository;
76: } catch (Exception ex) {
77: // in some cases it may be called outside http-request - for example in Lucene thread.
78: // in this case we should create repository by ourself (but do not cache it
79: UserSvnRepositoryFactory svnRepositoryFactory = new UserSvnRepositoryFactory();
80: svnRepositoryFactory.setRepositoryPath(m_repositoryPath);
81: svnRepositoryFactory
82: .setAnonymousProvider(m_anonymousProvider);
83:
84: try {
85: svnRepositoryFactory.afterPropertiesSet();
86: } catch (Exception ex2) {
87: // just ignore it for now
88: logger
89: .error("Cannot initialize request-scope svn repository factory");
90: }
91:
92: return svnRepositoryFactory.getSvnRepository();
93: }
94: }
95: }
|