Search in sources :

Example 1 with InstanceId

use of org.xwiki.instance.InstanceId in project xwiki-platform by xwiki.

the class DefaultInstanceIdManager method initialize.

@Override
public void initialize() {
    // Load it from the database
    XWikiContext context = getXWikiContext();
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStoreProvider.get();
    // Try retrieving the UUID from the database
    // First ensure that we're on the main wiki since we store the unique id only on the main wiki
    String originalDatabase = context.getWikiId();
    context.setWikiId(context.getMainXWiki());
    try {
        InstanceId id = store.failSafeExecuteRead(context, new XWikiHibernateBaseStore.HibernateCallback<InstanceId>() {

            @Override
            public InstanceId doInHibernate(Session session) throws HibernateException {
                // Retrieve the version from the database
                return (InstanceId) session.createCriteria(InstanceId.class).uniqueResult();
            }
        });
        // If the database doesn't hold the UUID then compute one and save it
        if (id == null) {
            // Compute UUID
            final InstanceId newId = new InstanceId(UUID.randomUUID().toString());
            // will be retried again next time the wiki is restarted.
            try {
                store.executeWrite(context, new XWikiHibernateBaseStore.HibernateCallback<Object>() {

                    @Override
                    public Object doInHibernate(Session session) throws HibernateException {
                        session.createQuery("delete from " + InstanceId.class.getName()).executeUpdate();
                        session.save(newId);
                        return null;
                    }
                });
            } catch (XWikiException e) {
                this.logger.warn("Failed to save Instance id to database. Reason: [{}]", ExceptionUtils.getRootCauseMessage(e));
            }
            id = newId;
        }
        this.instanceId = id;
    } finally {
        // Restore original database
        context.setWikiId(originalDatabase);
    }
}
Also used : InstanceId(org.xwiki.instance.InstanceId) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 2 with InstanceId

use of org.xwiki.instance.InstanceId in project xwiki-platform by xwiki.

the class InstanceIdConverterTest method convertToString.

@Test
public void convertToString() throws Exception {
    InstanceId id = new InstanceId("b6ad6165-daaf-41a1-8a3f-9aa81451c402");
    assertEquals("b6ad6165-daaf-41a1-8a3f-9aa81451c402", this.mocker.getComponentUnderTest().convertToString(id));
}
Also used : InstanceId(org.xwiki.instance.InstanceId) Test(org.junit.Test)

Example 3 with InstanceId

use of org.xwiki.instance.InstanceId in project xwiki-platform by xwiki.

the class DatePingDataProviderTest method provideData.

@Test
public void provideData() throws Exception {
    InstanceId id = new InstanceId(UUID.randomUUID().toString());
    InstanceIdManager idManager = this.mocker.getInstance(InstanceIdManager.class);
    when(idManager.getInstanceId()).thenReturn(id);
    JestClient client = mock(JestClient.class);
    SearchResult searchResult = new SearchResult(new Gson());
    String resultString = "{\n" + "   \"took\": 4,\n" + "   \"timed_out\": false,\n" + "   \"_shards\": {\n" + "      \"total\": 5,\n" + "      \"successful\": 5,\n" + "      \"failed\": 0\n" + "   },\n" + "   \"hits\": {\n" + "      \"total\": 2,\n" + "      \"max_score\": 0,\n" + "      \"hits\": []\n" + "   },\n" + "   \"aggregations\": {\n" + "      \"firstPingDate\": {\n" + "         \"value\": 1392854400000\n" + "      },\n" + "      \"serverTime\": {\n" + "         \"value\": 1393200000000\n" + "      }\n" + "   }\n" + "}";
    searchResult.setJsonString(resultString);
    searchResult.setJsonObject(new JsonParser().parse(resultString).getAsJsonObject());
    searchResult.setSucceeded(true);
    when(client.execute(any(Search.class))).thenReturn(searchResult);
    JestClientManager jestManager = this.mocker.getInstance(JestClientManager.class);
    when(jestManager.getClient()).thenReturn(client);
    Map<String, Object> data = this.mocker.getComponentUnderTest().provideData();
    assertEquals(2, data.size());
    assertEquals(4L, data.get("sinceDays"));
    assertEquals(1392854400000L, data.get("firstPingDate"));
}
Also used : InstanceIdManager(org.xwiki.instance.InstanceIdManager) InstanceId(org.xwiki.instance.InstanceId) Search(io.searchbox.core.Search) Gson(com.google.gson.Gson) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) JestClientManager(org.xwiki.activeinstalls.internal.JestClientManager) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Example 4 with InstanceId

use of org.xwiki.instance.InstanceId in project xwiki-platform by xwiki.

the class DistributionPingDataProviderTest method provideData.

@Test
public void provideData() throws Exception {
    InstanceId id = new InstanceId(UUID.randomUUID().toString());
    InstanceIdManager idManager = this.mocker.getInstance(InstanceIdManager.class);
    when(idManager.getInstanceId()).thenReturn(id);
    ExtensionId environmentExtensionId = new ExtensionId("environmentextensionid", "2.0");
    CoreExtension environmentExtension = mock(CoreExtension.class);
    when(environmentExtension.getId()).thenReturn(environmentExtensionId);
    CoreExtensionRepository CoreExtensionRepository = this.mocker.getInstance(CoreExtensionRepository.class);
    when(CoreExtensionRepository.getEnvironmentExtension()).thenReturn(environmentExtension);
    Map<String, Object> data = this.mocker.getComponentUnderTest().provideData();
    assertEquals(3, data.size());
    assertEquals("environmentextensionid", data.get("distributionId"));
    assertEquals("2.0", data.get("distributionVersion"));
    assertEquals(id.getInstanceId(), data.get("instanceId"));
}
Also used : InstanceIdManager(org.xwiki.instance.InstanceIdManager) CoreExtension(org.xwiki.extension.CoreExtension) InstanceId(org.xwiki.instance.InstanceId) CoreExtensionRepository(org.xwiki.extension.repository.CoreExtensionRepository) ExtensionId(org.xwiki.extension.ExtensionId) Test(org.junit.Test)

Example 5 with InstanceId

use of org.xwiki.instance.InstanceId in project xwiki-platform by xwiki.

the class InstanceIdConverterTest method convertFromString.

@Test
public void convertFromString() throws Exception {
    InstanceId id = this.mocker.getComponentUnderTest().convertToType(InstanceId.class, "b6ad6165-daaf-41a1-8a3f-9aa81451c402");
    assertEquals("b6ad6165-daaf-41a1-8a3f-9aa81451c402", id.getInstanceId());
}
Also used : InstanceId(org.xwiki.instance.InstanceId) Test(org.junit.Test)

Aggregations

InstanceId (org.xwiki.instance.InstanceId)5 Test (org.junit.Test)4 InstanceIdManager (org.xwiki.instance.InstanceIdManager)2 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)1 JestClient (io.searchbox.client.JestClient)1 Search (io.searchbox.core.Search)1 SearchResult (io.searchbox.core.SearchResult)1 HibernateException (org.hibernate.HibernateException)1 Session (org.hibernate.Session)1 JestClientManager (org.xwiki.activeinstalls.internal.JestClientManager)1 CoreExtension (org.xwiki.extension.CoreExtension)1 ExtensionId (org.xwiki.extension.ExtensionId)1 CoreExtensionRepository (org.xwiki.extension.repository.CoreExtensionRepository)1