use of org.b3log.latke.urlfetch.URLFetchService in project solo by b3log.
the class InitService method init.
/**
* Initializes Solo.
*
* <p>
* Initializes the followings in sequence:
* <ol>
* <li>Statistic</li>
* <li>Preference</li>
* <li>Administrator</li>
* </ol>
* </p>
*
* <p>
* We will try to initialize Solo 3 times at most.
* </p>
*
* <p>
* Posts "Hello World!" article and its comment while Solo initialized.
* </p>
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "userName": "",
* "userEmail": "",
* "userPassword": "", // Unhashed
* }
* </pre>
*
* @throws ServiceException service exception
*/
public void init(final JSONObject requestJSONObject) throws ServiceException {
if (isInited()) {
return;
}
final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
if (RuntimeEnv.LOCAL == runtimeEnv) {
LOGGER.log(Level.INFO, "Solo is running on [" + runtimeEnv + "] environment, database [{0}], creates " + "all tables", Latkes.getRuntimeDatabase());
if (RuntimeDatabase.H2 == Latkes.getRuntimeDatabase()) {
String dataDir = Latkes.getLocalProperty("jdbc.URL");
dataDir = dataDir.replace("~", System.getProperty("user.home"));
LOGGER.log(Level.INFO, "YOUR DATA will be stored in directory [" + dataDir + "], " + "please pay more attention to it~");
}
final List<CreateTableResult> createTableResults = JdbcRepositories.initAllTables();
for (final CreateTableResult createTableResult : createTableResults) {
LOGGER.log(Level.DEBUG, "Create table result[tableName={0}, isSuccess={1}]", new Object[] { createTableResult.getName(), createTableResult.isSuccess() });
}
}
int retries = MAX_RETRIES_CNT;
while (true) {
final Transaction transaction = userRepository.beginTransaction();
try {
final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
if (null == statistic) {
initStatistic();
initPreference(requestJSONObject);
initReplyNotificationTemplate();
initAdmin(requestJSONObject);
initLink();
}
transaction.commit();
break;
} catch (final Exception e) {
if (0 == retries) {
LOGGER.log(Level.ERROR, "Initialize Solo error", e);
throw new ServiceException("Initailize Solo error: " + e.getMessage());
}
// Allow retry to occur
--retries;
LOGGER.log(Level.WARN, "Retrying to init Solo[retries={0}]", retries);
} finally {
if (transaction.isActive()) {
transaction.rollback();
}
}
}
final Transaction transaction = userRepository.beginTransaction();
try {
helloWorld();
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Hello World error?!", e);
}
try {
final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
final HTTPRequest req = new HTTPRequest();
req.setURL(new URL(Latkes.getServePath() + "/blog/symphony/user"));
urlFetchService.fetch(req);
} catch (final Exception e) {
LOGGER.log(Level.TRACE, "Sync account failed");
}
pluginManager.load();
}
Aggregations