Search in sources :

Example 21 with ForumException

use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.

the class InstallAction method configureJDBCConnection.

private void configureJDBCConnection() {
    String username = this.getFromSession("dbUser");
    String password = this.getFromSession("dbPassword");
    String dbName = this.getFromSession("dbName");
    String host = this.getFromSession("dbHost");
    String type = this.getFromSession("database");
    String encoding = this.getFromSession("dbEncoding");
    String port = this.getFromSession("dbPort");
    String dbConfigFilePath = SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) + "/database/" + type + "/" + type + ".properties";
    Properties p = new Properties();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(dbConfigFilePath);
        p.load(fis);
    } catch (IOException e) {
        throw new ForumException(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    this.handleDatabasePort(p, port);
    // Write database information to the respective file
    p.setProperty(ConfigKeys.DATABASE_CONNECTION_HOST, host);
    p.setProperty(ConfigKeys.DATABASE_CONNECTION_USERNAME, username);
    p.setProperty(ConfigKeys.DATABASE_CONNECTION_PASSWORD, password);
    p.setProperty(ConfigKeys.DATABASE_CONNECTION_DBNAME, dbName);
    p.setProperty(ConfigKeys.DATABASE_CONNECTION_ENCODING, encoding);
    p.setProperty(ConfigKeys.DATABASE_CONNECTION_PORT, port);
    p.setProperty(ConfigKeys.DATABASE_DRIVER_NAME, type);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(dbConfigFilePath);
        p.store(fos, null);
    } catch (Exception e) {
        logger.warn("Error while trying to write to " + type + ".properties: " + e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
    }
    // Proceed to SystemGlobals / jforum-custom.conf configuration
    for (Enumeration e = p.keys(); e.hasMoreElements(); ) {
        String key = (String) e.nextElement();
        String value = p.getProperty(key);
        SystemGlobals.setValue(key, value);
        logger.info("Updating key " + key + " with value " + value);
    }
}
Also used : ForumException(net.jforum.exceptions.ForumException) Enumeration(java.util.Enumeration) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) DatabaseException(net.jforum.exceptions.DatabaseException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ForumException(net.jforum.exceptions.ForumException)

Example 22 with ForumException

use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.

the class ParseDBStructFile method parse.

public static List parse(String filename) {
    List statements = new ArrayList();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(filename));
        StringBuffer sb = new StringBuffer(512);
        boolean processing = false;
        char delimiter = ';';
        String[] creators = { "CREATE INDEX", "CREATE TABLE", "CREATE SEQUENCE", "DROP TABLE", "IF EXISTS", "DROP SEQUENCE", "DROP INDEX" };
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.length() == 0) {
                continue;
            }
            char charAt = line.charAt(0);
            // Ignore comments
            if (charAt == '-' || charAt == '#') {
                continue;
            }
            if (processing) {
                sb.append(line);
                if (line.indexOf(delimiter) > -1) {
                    sb.delete(sb.length() - 1, sb.length());
                    statements.add(sb.toString());
                    processing = false;
                }
            } else {
                for (int i = 0; i < creators.length; i++) {
                    if (line.indexOf(creators[i]) > -1) {
                        sb.delete(0, sb.length());
                        if (line.indexOf(delimiter) > -1) {
                            if (line.indexOf(';') > -1) {
                                line = line.replace(';', ' ');
                            }
                            statements.add(line);
                        } else {
                            sb.append(line);
                            processing = true;
                        }
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ForumException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            // catch close BufferedReader
            }
        }
    }
    return statements;
}
Also used : ForumException(net.jforum.exceptions.ForumException) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) ForumException(net.jforum.exceptions.ForumException)

Example 23 with ForumException

use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.

the class PostAction method insert.

public void insert() {
    int forumId;
    // If we have a topic_id, then it should be a reply
    if (this.request.getParameter("topic_id") != null) {
        int topicId = this.request.getIntParameter("topic_id");
        Topic t = TopicRepository.getTopic(new Topic(topicId));
        if (t == null) {
            t = DataAccessDriver.getInstance().newTopicDAO().selectRaw(topicId);
            if (t == null) {
                throw new ForumException("Could not find a topic with id #" + topicId);
            }
        }
        forumId = t.getForumId();
        if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
            return;
        }
        if (t.getStatus() == Topic.STATUS_LOCKED) {
            this.topicLocked();
            return;
        }
        this.context.put("topic", t);
        this.context.put("setType", false);
        this.context.put("pageTitle", I18n.getMessage("PostForm.reply") + " " + t.getTitle());
    } else {
        forumId = this.request.getIntParameter("forum_id");
        if (this.isReplyOnly(forumId)) {
            this.replyOnly();
            return;
        }
        this.context.put("setType", true);
        this.context.put("pageTitle", I18n.getMessage("PostForm.title"));
    }
    Forum forum = ForumRepository.getForum(forumId);
    if (forum == null) {
        throw new ForumException("Could not find a forum with id #" + forumId);
    }
    if (!TopicsCommon.isTopicAccessible(forumId)) {
        return;
    }
    if (!this.anonymousPost(forumId) || this.isForumReadonly(forumId, this.request.getParameter("topic_id") != null)) {
        return;
    }
    int userId = SessionFacade.getUserSession().getUserId();
    this.setTemplateName(TemplateKeys.POSTS_INSERT);
    // Attachments
    boolean attachmentsEnabled = SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(forumId));
    if (attachmentsEnabled && !SessionFacade.isLogged() && !SystemGlobals.getBoolValue(ConfigKeys.ATTACHMENTS_ANONYMOUS)) {
        attachmentsEnabled = false;
    }
    this.context.put("attachmentsEnabled", attachmentsEnabled);
    if (attachmentsEnabled) {
        QuotaLimit ql = new AttachmentCommon(this.request, forumId).getQuotaLimit(userId);
        this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
        this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
    }
    boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS);
    this.context.put("moderationLoggingEnabled", SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED));
    this.context.put("smilies", SmiliesRepository.getSmilies());
    this.context.put("forum", forum);
    this.context.put("action", "insertSave");
    this.context.put("start", this.request.getParameter("start"));
    this.context.put("isNewPost", true);
    this.context.put("needCaptcha", needCaptcha);
    this.context.put("htmlAllowed", SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, Integer.toString(forumId)));
    this.context.put("canCreateStickyOrAnnouncementTopics", SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS));
    this.context.put("canCreatePolls", SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_POLL));
    User user = DataAccessDriver.getInstance().newUserDAO().selectById(userId);
    ViewCommon.prepareUserSignature(user);
    if (this.request.getParameter("preview") != null) {
        user.setNotifyOnMessagesEnabled(this.request.getParameter("notify") != null);
    }
    this.context.put("user", user);
}
Also used : ForumException(net.jforum.exceptions.ForumException) User(net.jforum.entities.User) Topic(net.jforum.entities.Topic) QuotaLimit(net.jforum.entities.QuotaLimit) AttachmentCommon(net.jforum.view.forum.common.AttachmentCommon) Forum(net.jforum.entities.Forum)

Example 24 with ForumException

use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.

the class I18n method load.

static void load(String localeName, String mergeWith, boolean force) {
    if (!force && (localeName == null || localeName.trim().equals("") || I18n.contains(localeName))) {
        return;
    }
    if (localeNames.size() == 0) {
        loadLocales();
    }
    Properties p = new Properties();
    if (mergeWith != null) {
        if (!I18n.contains(mergeWith)) {
            load(mergeWith, null);
        }
        p.putAll((Properties) messagesMap.get(mergeWith));
    }
    FileInputStream fis = null;
    try {
        String filename = baseDir + localeNames.getProperty(localeName);
        // If the requested locale does not exist, use the default
        if (!new File(filename).exists()) {
            filename = baseDir + localeNames.getProperty(SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT_ADMIN));
        }
        fis = new FileInputStream(filename);
        p.load(fis);
    } catch (IOException e) {
        throw new ForumException(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    messagesMap.put(localeName, p);
    watchForChanges(localeName);
}
Also used : ForumException(net.jforum.exceptions.ForumException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ForumException(net.jforum.exceptions.ForumException)

Example 25 with ForumException

use of net.jforum.exceptions.ForumException in project jforum2 by rafaelsteil.

the class LuceneReindexer method reindex.

private void reindex() {
    try {
        if (recreate) {
            this.settings.createIndexDirectory(SystemGlobals.getValue(ConfigKeys.LUCENE_INDEX_WRITE_PATH));
        }
    } catch (IOException e) {
        throw new ForumException(e);
    }
    LuceneDAO dao = DataAccessDriver.getInstance().newLuceneDAO();
    IndexSearcher searcher = null;
    LuceneSearch luceneSearch = ((LuceneManager) SearchFacade.manager()).luceneSearch();
    LuceneIndexer luceneIndexer = ((LuceneManager) SearchFacade.manager()).luceneIndexer();
    int fetchCount = SystemGlobals.getIntValue(ConfigKeys.LUCENE_INDEXER_DB_FETCH_COUNT);
    try {
        if (!recreate) {
            searcher = new IndexSearcher(this.settings.directory());
        }
        boolean hasMorePosts = true;
        long processStart = System.currentTimeMillis();
        int firstPostId = args.filterByMessage() ? args.getFirstPostId() : dao.firstPostIdByDate(args.getFromDate());
        if (args.filterByMessage()) {
            int dbFirstPostId = dao.firstPostId();
            if (firstPostId < dbFirstPostId) {
                firstPostId = dbFirstPostId;
            }
        }
        int lastPostId = args.filterByMessage() ? args.getLastPostId() : dao.lastPostIdByDate(args.getToDate());
        int counter = 1;
        int indexTotal = 0;
        long indexRangeStart = System.currentTimeMillis();
        while (hasMorePosts) {
            boolean contextFinished = false;
            int toPostId = firstPostId + fetchCount < lastPostId ? firstPostId + fetchCount : lastPostId;
            try {
                JForumExecutionContext ex = JForumExecutionContext.get();
                JForumExecutionContext.set(ex);
                List l = dao.getPostsToIndex(firstPostId, toPostId);
                if (counter >= 5000) {
                    long end = System.currentTimeMillis();
                    System.out.println("Indexed ~5000 documents in " + (end - indexRangeStart) + " ms (" + indexTotal + " so far)");
                    indexRangeStart = end;
                    counter = 0;
                }
                JForumExecutionContext.finish();
                contextFinished = true;
                for (Iterator iter = l.iterator(); iter.hasNext(); ) {
                    if ("0".equals(SystemGlobals.getValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING))) {
                        hasMorePosts = false;
                        break;
                    }
                    Post post = (Post) iter.next();
                    if (!recreate && args.avoidDuplicatedRecords()) {
                        if (luceneSearch.findDocumentByPostId(post.getId()) != null) {
                            continue;
                        }
                    }
                    luceneIndexer.batchCreate(post);
                    counter++;
                    indexTotal++;
                }
                firstPostId += fetchCount;
                hasMorePosts = hasMorePosts && l.size() > 0;
            } finally {
                if (!contextFinished) {
                    JForumExecutionContext.finish();
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("**** Total: " + (end - processStart) + " ms");
    } catch (IOException e) {
        throw new ForumException(e);
    } finally {
        SystemGlobals.setValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING, "0");
        luceneIndexer.flushRAMDirectory();
        if (searcher != null) {
            try {
                searcher.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) JForumExecutionContext(net.jforum.JForumExecutionContext) Post(net.jforum.entities.Post) LuceneDAO(net.jforum.dao.LuceneDAO) IOException(java.io.IOException) IOException(java.io.IOException) ForumException(net.jforum.exceptions.ForumException) ForumException(net.jforum.exceptions.ForumException) Iterator(java.util.Iterator) List(java.util.List)

Aggregations

ForumException (net.jforum.exceptions.ForumException)37 IOException (java.io.IOException)24 FileInputStream (java.io.FileInputStream)10 File (java.io.File)7 Iterator (java.util.Iterator)6 Properties (java.util.Properties)6 DatabaseException (net.jforum.exceptions.DatabaseException)6 List (java.util.List)5 SQLException (java.sql.SQLException)4 CacheEngineStartupException (net.jforum.exceptions.CacheEngineStartupException)4 SchedulerException (org.quartz.SchedulerException)4 FileOutputStream (java.io.FileOutputStream)3 PreparedStatement (java.sql.PreparedStatement)3 Enumeration (java.util.Enumeration)3 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 SAXParser (javax.xml.parsers.SAXParser)2 Post (net.jforum.entities.Post)2 User (net.jforum.entities.User)2 SSO (net.jforum.sso.SSO)2