You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by aledsage <gi...@git.apache.org> on 2016/11/16 23:31:37 UTC
[GitHub] brooklyn-server pull request #442: BROOKLYN-379: Fix rebind when bundle pref...
GitHub user aledsage opened a pull request:
https://github.com/apache/brooklyn-server/pull/442
BROOKLYN-379: Fix rebind when bundle prefixes are used to load classes
This replaces https://github.com/apache/brooklyn-server/pull/396
See also https://github.com/neykov/brooklyn-server/pull/4 (but I'll replay all the comments from that PR into this PR).
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/aledsage/brooklyn-server neykov-aled/test/osgi-prefix-rebind
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/442.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #442
----
commit f4f008598f9f4ed1744c1d632d0b669c5a2c4434
Author: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Date: 2016-10-31T15:22:37Z
A test to check rebind when bundle prefixes are used to load classes in catalog items
commit 2d2f90332a3c9822eafc01403518f614ba8eca05
Author: Aled Sage <al...@gmail.com>
Date: 2016-11-07T17:11:45Z
BROOKLYN-379: entity type includes OSGi prefix
commit 49b6535f439bbca5d74f69f0439be5ceeeae6a78
Author: Aled Sage <al...@gmail.com>
Date: 2016-11-07T18:03:30Z
Fix osgi resolveClass to strip bundle prefix
Without this, RebindOsgiTest.testUsesCatalogBundleVersion fails
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #442: BROOKLYN-379: Fix rebind when bundle pref...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/442#discussion_r89816716
--- Diff: core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java ---
@@ -189,10 +194,15 @@ public static boolean isBundleNameEqualOrAbsent(CatalogBundle bundle, Bundle b)
Maybe<Bundle> bundle = findBundle(osgiBundle);
if (bundle.isPresent()) {
Bundle b = bundle.get();
- Class<T> clazz;
+ Optional<String> strippedType = osgiClassPrefixer.stripMatchingPrefix(b, type);
+ String typeToLoad = strippedType.isPresent() ? strippedType.get() : type;
+ if (osgiClassPrefixer.hasPrefix(typeToLoad)) {
+ bundleProblems.put(osgiBundle, new UserFacingException("Bundle does not match prefix in type name '"+typeToLoad+"'"));
+ continue;
+ }
--- End diff --
`getResource(s)` needs the same update. Turns out we can't load prefixed resources when combined with `brooklyn.libraries`.
Could be addressed as another PR.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #442: BROOKLYN-379: Fix rebind when bundle pref...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/442#discussion_r88356229
--- Diff: core/src/main/java/org/apache/brooklyn/core/mgmt/persist/OsgiClassPrefixer.java ---
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.persist;
+
+import org.apache.brooklyn.core.mgmt.rebind.dto.MementosGenerators;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.osgi.framework.Bundle;
+
+import com.google.common.base.Optional;
+
+/**
+ * For serialize/deserialize of persisted state, we need to prefix classes with their OSGi bundle
+ * (so we know which classloader to use when deserializing). In most cases, this can be done when
+ * serializing the instances. However, in some cases the field of a memento is the String representation
+ * of the type (e.g. see (@link {@link org.apache.brooklyn.core.mgmt.rebind.dto.AbstractMemento#getType()}).
+ * In that situation, the code writing the field must include the prefix (e.g. see
+ * {@link MementosGenerators}.
+ */
+public class OsgiClassPrefixer {
+
+ private final ClassLoaderUtils whiteListRetriever;
+
+ public OsgiClassPrefixer() {
+ whiteListRetriever = new ClassLoaderUtils(getClass());
+ }
+
+ public Optional<String> getPrefix(Class<?> type) {
+ Optional<Bundle> bundle = Osgis.getBundleOf(type);
+ if (bundle.isPresent() && !whiteListRetriever.isBundleWhiteListed(bundle.get())) {
+ return Optional.of(bundle.get().getSymbolicName() + ":");
+ }
+ return Optional.absent();
+ }
+
+ public Optional<String> stripMatchingPrefix(Bundle bundle, String type) {
+ String symbolicName = bundle.getSymbolicName();
+ if (symbolicName != null && type.startsWith(symbolicName + ":")) {
+ return Optional.of(type.substring((symbolicName + ":").length()));
--- End diff --
@neykov wrote: The prefix could include version identifier as well.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #442: BROOKLYN-379: Fix rebind when bundle pref...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/442#discussion_r88418644
--- Diff: core/src/main/java/org/apache/brooklyn/core/mgmt/persist/OsgiClassPrefixer.java ---
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.persist;
+
+import org.apache.brooklyn.core.mgmt.rebind.dto.MementosGenerators;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.osgi.framework.Bundle;
+
+import com.google.common.base.Optional;
+
+/**
+ * For serialize/deserialize of persisted state, we need to prefix classes with their OSGi bundle
+ * (so we know which classloader to use when deserializing). In most cases, this can be done when
+ * serializing the instances. However, in some cases the field of a memento is the String representation
+ * of the type (e.g. see (@link {@link org.apache.brooklyn.core.mgmt.rebind.dto.AbstractMemento#getType()}).
+ * In that situation, the code writing the field must include the prefix (e.g. see
+ * {@link MementosGenerators}.
+ */
+public class OsgiClassPrefixer {
+
+ private final ClassLoaderUtils whiteListRetriever;
+
+ public OsgiClassPrefixer() {
+ whiteListRetriever = new ClassLoaderUtils(getClass());
+ }
+
+ public Optional<String> getPrefix(Class<?> type) {
+ Optional<Bundle> bundle = Osgis.getBundleOf(type);
+ if (bundle.isPresent() && !whiteListRetriever.isBundleWhiteListed(bundle.get())) {
--- End diff --
On the plus side, this is just moving the white list code (rather than proliferating). It is deleted from `XmlMementoSerializer.OsgiClassnameMapper`, which now delegates to this code. It does mean the white-list code is called slightly more often, but thus in a more consistent manner for handling prefixes.
Let's revisit the white list in a separate PR (potentially on the dev@brooklyn mailing list first).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #442: BROOKLYN-379: Fix rebind when bundle prefixes ar...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:
https://github.com/apache/brooklyn-server/pull/442
@neykov can you review this again please?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #442: BROOKLYN-379: Fix rebind when bundle pref...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/brooklyn-server/pull/442
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #442: BROOKLYN-379: Fix rebind when bundle prefixes ar...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:
https://github.com/apache/brooklyn-server/pull/442
LGTM, merging.
Resource handling can be added in another PR.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---